The log4j can be configured both programmatically and externally using special configuration files. External configuration is most preferred, because
to take effect it doesn’t require change in application code, recompilation, or redeployment. Configuration files can be XML files or Java property files that can be created and edited using any text editor or XML editor, respectively.

1. Programmatically configure log4j

This example is from this site, the code is messy, so why not make it clean.

package Test;
 
import org.apache.log4j.*;
 
public class TestLog {
 
/*
* get a static logger instance with name TestLog
*/

static Logger myLogger = Logger.getLogger(TestLog.class.getName());
Appender myAppender;
SimpleLayout myLayout;
 
/* Constructor */
public TestLog() {
 
/*
* Set logger priority level programmatically. Though this is better done externally
*/

myLogger.setLevel(Level.ALL);
 
/*
* Instantiate a layout and an appender, assign layout to appender
* programmatically
*/

myLayout = new SimpleLayout();
myAppender = new ConsoleAppender(myLayout); // Appender is Interface
 
/* Assign appender to the logger programmatically */
myLogger.addAppender(myAppender);
 
} // end constructor
 
public void do_something(int a, float b) {
 
/*
* This log request enabled and log statement logged, since INFO = INFO
*/

myLogger.info("The values of parameters passed to method do_something are: "+ a + ", " + b);
 
/* this log request is not enabled, since DEBUG < INFO */ myLogger.debug("Operation performed successfully"); Object x=null; if (x == null) { /* * this log request is enabled and log statement logged, since ERROR * > INFO
*/

myLogger.error("Value of X is null");
 
}
} // end do_something()
public static void main(String []args){
new TestLog().do_something(1, 3);
}
} // end class MyClass

Here is the snapshot of output:

2. Configure Log4j externally

Create a file named “log4j.properties” in the “src” directory of your project.

# Define the root logger with appender file
log = /home/ryan/Desktop/log4j
log4j.rootLogger = DEBUG, FILE # Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out # Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

Here is the java code.

package Test;
import org.apache.log4j.Logger;
public class TestLogger {
static Logger myLogger = Logger.getLogger(TestLog.class.getName());
 
public static void main(String[] args) {
myLogger.info("testing");
myLogger.warn("warning testing");
myLogger.error("this is an error");
}
 
}

The log will be printed on the log.out file in /home/ryan/Desktop/log4j directory.

An Entry Example of Log4j的更多相关文章

  1. lucene构建restful风格的简单搜索引擎服务

    来自于本人博客: lucene构建restful风格的简单搜索引擎服务 本人的博客如今也要改成使用lucene进行全文检索的功能,因此在这里把代码贴出来与大家分享 一,文件夹结构: 二,配置文件: 总 ...

  2. Jetty启动报Error scanning entry META-INF/versions/9/org/apache/logging/log4j/util/ProcessIdUtil.class

    近日在项目中集成Elasticsearch后,Jetty启动报错. 错误日志如下: Suppressed: |java.lang.RuntimeException: Error scanning en ...

  3. log4j 文档

    log4j中文文档  中文详细教程 log4j中文文档   这篇文章描述了Log4j的API.独一无二的特色和设计原理.Log4j是一个聚集了许多作者劳动成果的开源软件项目.它允许开发人眼以任意的粒度 ...

  4. Log4J基础详解及示例大全

    去年这个时候,为做软件工程的大作业就详细学过Log4J的用法了,时隔一年想要在新的项目中好好使用一下的时候,发现几乎全忘了,悲催啊-- 再上网查资料,总是不能找到一篇符合我的口味,拿来就能轻松上手,方 ...

  5. Log4j的ConversionPattern无缝适配到Logback

    为了能将log4j的ConversionPattern无缝应用到logback上来,需要对两个Conversion做适配,具体可以参考:Log4j 与 Logback的ConversionPatter ...

  6. log4j使用教程详解(怎么使用log4j2)

    1. 去官方下载log4j 2,导入jar包,基本上你只需要导入下面两个jar包就可以了(xx是乱七八糟的版本号): log4j-core-xx.jar log4j-api-xx.jar 2. 导入到 ...

  7. Log4j 2使用教程

    Log4j 2的好处就不和大家说了,如果你搜了2,说明你对他已经有一定的了解,并且想用它,所以这里直接就上手了. 1. 去官方下载log4j 2,导入jar包,基本上你只需要导入下面两个jar包就可以 ...

  8. log4j 总结 精华

    去年这个时候,为做软件工程的大作业就详细学过Log4J的用法了,时隔一年想要在新的项目中好好使用一下的时候,发现几乎全忘了,悲催啊…… 再上网查资料,总是不能找到一篇符合我的口味,拿来就能轻松上手,方 ...

  9. log4j.properties文件配置--官方文档

    Default Initialization Procedure The log4j library does not make any assumptions about its environme ...

  10. Log4j 2.0 使用说明

      原文地址:http://blog.csdn.net/welcome000yy/article/details/7962447 Log4j 2.0 使用说明(1) 之HelloWorld 最近刚接触 ...

随机推荐

  1. C# – class, filed, property, const, readonly, get, set, init, required 使用基础

    前言 心血来潮,这篇讲点基础的东西. Field 比起 Property,Field 很不起眼,你若问 JavaScript,它甚至都没有 Field. 但在 C#,class 里头真正装 value ...

  2. JavaScript – Function 函数

    参考 阮一峰 – 函数的扩展 基本用法 function fn1(param1, param2 = 'default value') { return 'return value'; } fn1('1 ...

  3. CSS & JS Effect – Hamburger Menu

    效果 参考: Youtube – Responsive Navigation Menu Bar + Hamburger Menu Toggle - Only with CSS Youtube – Ma ...

  4. C++ STL stack容器——栈

    stack容器 基本概念 stack是一种先进后出的数据结构,它只有一个出口,形式如下图所示.stack容器允许新增元素,移除元素,取得栈顶元素,但是除了最顶端外,没有任何地方可以存取stack的娶她 ...

  5. [OI] pb_ds

    using namespace __gnu_pbds; Luogu Post#39 1.堆 1.1 基本信息 头文件 #include <ext/pb_ds/priority_queue.hpp ...

  6. 【赵渝强老师】在MongoDB中使用游标

    一.什么是游标? 游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次一行或者多行前进或向后浏览数据的能力. 游标实际上是一种能从包括多条数据记录的结果集 ...

  7. oh-my-zsh nvm command not found

    oh-my-zsh nvm command not found 如果你在使用 oh-my-zsh 并且在终端输入 nvm 命令时提示 "command not found",这可能 ...

  8. 架构与思维:漫谈高并发业务的CAS及ABA

    1 高并发场景下的难题 1.1 典型支付场景 这是最经典的场景.支付过程,要先查询买家的账户余额,然后计算商品价格,最后对买家进行进行扣款,像这类的分布式操作, 如果是并发量低的情况下完全没有问题的, ...

  9. flops, params = profile(model, inputs=(x,))计算

    计算量:FLOPs,FLOP时指浮点运算次数,s是指秒,即每秒浮点运算次数的意思,考量一个网络模型的计算量的标准.参数量:Params,是指网络模型中需要训练的参数总数. flops(G) = flo ...

  10. markdown.css 设置文章的样式

    返回的详情文章内容是标签加内容文字,使用 markdown,css 渲染样式 : .markdown-body .octicon { display: inline-block; fill: curr ...