Log4J的配置文件(Configuration File)就是用来设置记录器的级别、存放器和布局的,它可接key=value格式的设置或xml格式的设置信息。通过配置,可以创建出Log4J的运行环境。Log4j的配置文件是一个叫做
Log4j.properties的文件。
log4j.properties文件:
#newhappy log4j.properties start
log4j.rootLogger=DEBUG,myConsole,myLogFile
#console appender
log4j.appender.myConsole=org.apache.log4j.ConsoleAppender
log4j.appender.myConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsole.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n
log4j.appender.myConsole.threshold=FATAL
#rolling file appender
log4j.appender.myLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.myLogFile.File=mylog.log
log4j.appender.myLogFile.MaxFileSize=100KB
log4j.appender.myLogFile.MaxBackupIndex=2
log4j.appender.myLogFile.layout=org.apache.log4j.PatternLayout
log4j.appender.myLogFile.layout.ConversionPattern=%d{mmm d,yyyy hh:mm:ss a} : %p [%t] %m%n
log4j.appender.myLogFile.threshold=ERROR
#console appender
log4j.appender.myConsole=org.apache.log4j.ConsoleAppender
log4j.appender.myConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsole.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n
log4j.appender.myConsole.threshold=FATAL
#rolling file appender
log4j.appender.myLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.myLogFile.File=mylog.log
log4j.appender.myLogFile.MaxFileSize=100KB
log4j.appender.myLogFile.MaxBackupIndex=2
log4j.appender.myLogFile.layout=org.apache.log4j.PatternLayout
log4j.appender.myLogFile.layout.ConversionPattern=%d{mmm d,yyyy hh:mm:ss a} : %p [%t] %m%n
log4j.appender.myLogFile.threshold=ERROR
#newhappy log4j.properties end
1. 配置文件
Log4J配置文件的基本格式如下:
#配置根Logger
log4j.rootLogger = [ level ] , appenderName1 , appenderName2 ,
…
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
#配置日志信息输出目的地Appender
log4j.appender.appenderName =
fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 =
value1
…
log4j.appender.appenderName.optionN =
valueN
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
#配置日志信息的格式(布局)
log4j.appender.appenderName.layout =
fully.qualified.name.of.layout.class
log4j.appender.appenderName.layout.option1 =
value1
…
log4j.appender.appenderName.layout.optionN = valueN
Log4J配置文件的基本格式如下:
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
其中 [level] 是日志输出级别,共有5级:
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
Appender 为日志输出目的地,Log4j提供的appender有以下几种:
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
Layout:日志输出格式,Log4j提供的layout有以下几种:
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
打印参数: Log4J采用类似C语言中的printf函数的打印格式格式化日志信息,如下:
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
2. 在代码中初始化Logger:
1)在程序中调用BasicConfigurator.configure()方法:给根记录器增加一个ConsoleAppender,输出格式通过PatternLayout设为"%-4r [%t] %-5p %c %x - %m%n",还有根记录器的默认级别是Level.DEBUG.
2)配置放在文件里,通过命令行参数传递文件名字,通过PropertyConfigurator.configure(args[x])解析并配置;
3)配置放在文件里,通过环境变量传递文件名等信息,利用log4j默认的初始化过程解析并配置;
4)配置放在文件里,通过应用服务器配置传递文件名等信息,利用一个特殊的servlet来完成配置。
3. 为不同的 Appender 设置日志输出级别:
当调试系统时,我们往往注意的只是异常级别的日志输出,但是通常所有级别的输出都是放在一个文件里的,如果日志输出的级别是BUG!?那就慢慢去找吧。
这时我们也许会想要是能把异常信息单独输出到一个文件里该多好啊。当然可以,Log4j已经提供了这样的功能,我们只需要在配置中修改Appender的Threshold 就能实现,比如下面的例子:
[配置文件]
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
log4j.appender.D.File = logs/error.log ## 异常日志文件名
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## 只输出ERROR级别以上的日志!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404145997.gif?x-oss-process=image/resize,w_1400/format,webp)
[代码中使用]
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150094.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150050.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150069.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150050.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150032.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150032.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150032.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150032.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150069.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://www.xue5.com/itedu/UploadFiles/200802/2008022404150087.gif?x-oss-process=image/resize,w_1400/format,webp)
运行一下,看看异常信息是不是保存在了一个单独的文件error.log中。
4、对不同的包设置不同输出信息级别
eg:
打印OppenSessionInViewFilter中debug级别信息方法:
log4j.logger.org.springframework.orm.hibernate3.support.OpenSessionInViewFilter=debug
打印Struts标签中warn级别信息的方法:
log4j.logger.org.apache.struts.util=WARN
原理:
Suppose we are no longer interested in seeing the output of any component belonging to the com.foo package. The following configuration file shows one possible way of achieving this.
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
本文转自 牛海彬 51CTO博客,原文链接:http://blog.51cto.com/newhappy/135703,如需转载请自行联系原作者