Log4j will be usually configured using a properties file or xml file externally. So once the log statements are in place you can easily control them using the external configuration file without modifying the source code. Now let’s see how you can obtain the same log output as the previous example using the properties file configuration.
Log4j通常采用一个properties文件或者xml文件从外部进行配置。所以一旦你需要更改日志的声明,只需要修改外部文件而不必修改源代码。现在,让我们看看如何使用一个properties配置文件来实现前面的例子,得到一样的输出结果。
Three main components you need to configure to obtain the same result are the logger, appender and layout. The logger object is the one that is used to log messages, appender is the one that specifies the output destination like console or a file and layout is the one that specify the format in which the log messages should be logged.
要实现和前面例子一样,logger、appender和layout是你需要进行配置的三大核心要素。logger是用来记录信息,appender是用来指定输出方向,例如控制台或者文件,layout是来指定输出的日志的格式。
When you configure using the BasicConfigurator.configure() method by default it uses the ConsoleAppender and PatternLayout for all the loggers.
The following configuration creates the same result as the BasicConfigurator.configure() method.
当你使用BasicConfigurator.configure()方法时,模式将使用ConsoleAppender (控制台输出)和PatternLayout进行输出。
一下的配置将使用BasicConfigurator.configure()时同样的效果。
log4j.rootLogger=DEBUG, CA log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
The rootLogger is the one that resides on the top of the logger hierarchy. Here we set its level to DEBUG and added the console appender (CA) to it. The console appender can have arbitrary name, here its name is CA.
rootLogger是日志的跟记录器,代表日志的最高层。我们设置他的级别为DEBUG,设定输出为控制台输出。控制台的输出器可以有一个专有的名字。这里的名字是CA。
You need to create an appender as shown and set its layout to PatternLayout. The PatternLayout uses the ConversionPattern to format the message. To know more about the various formating options you can refer the documentation. ( PatternLayout )
设置外设计器后,我们需要设置它的layout为PatternLayout。它的PatternLayout使用ConversionPattern来格式化信息。想了解更多的格式化选项可以去看下参考文档(PatternLayout ) )
Once the appender is created and its layout is set you need to specify which loggers can use this appender. If you set this appender to the rootLogger then all the loggers will log messge to this appender. Since the rootLogger is on top of the hierarchy all the loggers will inherit its logger level and its appenders. You will see this in more detail in the comming examples.
Here is our example code.
当appender创建并且layout也设置好了之后要制定那个日志记录器可以使用这个apperder。如果你设置这个appender指定为rootLogger,那么所有的日志记录器将使用这个appender来进行记录信息。因为rootLogger处于所有日志记录器的最高层(即所有的日志记录器都将继承它),内置了所有的的日志级别。我们可以从接下里的例子看到更多的细节。
以下是示例代码
package com.vaannila.helloworld;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class HelloWorld {
static final Logger logger = Logger.getLogger(HelloWorld.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
}
}
You need to use the PropertyConfigurator.configure() method to configure log4j using a properties file. Log4j should be configured only once for the entire application.
你需要使用PropertyConfigurator.configure() 方法配合properties文件来一起配置log4j。Log4j在整个应用中只需配置一次。
When you execute the example the following output will be displayed on the console.
当你执行这个例子后,将在控制台上显示一下信息
0 [main] DEBUG com.vaannila.helloworld.HelloWorld - Sample debug message 0 [main] INFO com.vaannila.helloworld.HelloWorld - Sample info message 0 [main] WARN com.vaannila.helloworld.HelloWorld - Sample warn message 0 [main] ERROR com.vaannila.helloworld.HelloWorld - Sample error message 0 [main] FATAL com.vaannila.helloworld.HelloWorld - Sample fatal message
Since the rootLogger level is set to DEBUG all the messages are displayed.
因为rootLogger的水平设置为DEBUG,所以所有的信息均输出。
The log4j levels follow the following order.
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
If you set the rootLogger level to WARN then only the WARN, ERROR and FATAL level log messages will be displayed and the rest will be dropped.
如果你设置rootLogger的水平为WARN,那么只有WARN,ERROR和FATAL水平的日志会输出。
log4j.rootLogger=WARN, CA
0 [main] WARN com.vaannila.helloworld.HelloWorld - Sample warn message 15 [main] ERROR com.vaannila.helloworld.HelloWorld - Sample error message 15 [main] FATAL com.vaannila.helloworld.HelloWorld - Sample fatal message
The directory structure of the project is shown below.
整个项目的目录结构如下:
You can download the log4j configuration file here.
Source :Download
Source + Lib :Download
本文的原文地址为:http://www.vaannila.com/log4j/sample-log4j-properties-file-configuration-1.html
译者注:
1、Log4j常用的优先级FATAL>ERROR>WARN>INFO>DEBUG
2、layout,翻译成中文是布局,但本来感觉意思还是不够贴切,就不翻译了。log4j提供一下四种布局
org.apache.log4j.HTMLLayout(以HTML表格形式布局)
org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
3、appender也就是日志输出的目的地。

