Original address: http://blog.csdn.net/naruto1021/article/details/7969535
When using the spring framework, we can easily configure the log4j for log management.
Before configuring, you must first download the jar package (Log4j-1.2.11.jar)
The configuration is as follows:
1. At the top of Web. XML, add the following configuration code:
[HTML] View Plain copy print? <!--log4j configuration file load--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/web-inf/ log4j.properties</param-value> </context-param > <!--Start a watchdog thread scan every 1800 seconds for changes in log4j configuration files--> <context-param> < param-name>log4jrefreshinterval</param-name> <param-value>1800000</param-value> </context-param> <!--spring log4j listener-->& NBsp; <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
<!--log4j configuration file Load--
<context-param>
<param-name>log4jconfiglocation</param-name >
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<! --Start a watchdog thread scan every 1800 seconds log4j configuration file Changes-
<context-param>
<param-name> log4jrefreshinterval</param-name>
<param-value>1800000</param-value>
</ Context-param>
<!--spring log4j listener--
<listener>
<listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class>
</listener>
2. Place the log4j.properties configuration file in the specified directory configured above
Log4j.properties configuration file:
[Plain] View Plain copy print? ###################################### # LOG4J Configuration related Instructions ###################################### #%p output priority, i.e. debug, info,warn,error,fatal #%r Output The number of milliseconds that the log information is consumed from the application to output the class that the #%c output belongs to, Usually the full name of the class #%t output the thread name that generated the log event the information specified in the #%m output code #%n Output a carriage return line break, the Windows platform is "\ r \ n", the UNIX platform is "\ n" #%d the date or time of the output log point, the default format is ISO8601, you can specify the format later, such as:%d{ YYYY&NBSP;MM&NBSP;DD&NBSP;HH:MM:SS,SSS}, output similar: 2002 October 18 22:10:28,921 #%l The location where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10) #log4j提供4种布局: # Org.apache.log4j.HTMLLayout (layout as HTML table) #org. Apache.log4j.PatternLayout (flexibility to specify layout mode), # Org.apache.log4j.SimpleLayout (contains the level and information string for log information), #org. apache.log4j.Ttcclayout (contains information about the time, thread, category, and so on for log generation #log4j中有五级logger Output level: #FATAL 0 #ERROR 3 #WARN 4 #INFO 6 # debug 7 ###################################### # &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG4J Related Configuration ###################################### #日志输出级别 log4j.rootlogger=info,stdout,other # Set the log output console for stdout log4j.appender.stdout=org.apache.log4j.consoleappender #输出日志到控制台的方式, Default is system.out log4j.appender.stdout.target = system.out #设置使用灵活布局 log4j.appender.stdout.layout=org.apache.log4j.patternlayout #灵活定义输出格式 log4j.appender.stdout.layout.conversionpattern=[%p][%d{yyyy-mm-dd hh:mm:ss}] %l %m %n #设Log output console log4j.appender.other=org.apache.log4j.rollingfileappender #设置other的输出日志 log4j.appender.other.file=${webapp.root}/web-inf/logs/log.log #设置other的日志最大限制 log4j.appender.other.maxfilesize=1024kb #最多只保存20个备份文件 log4j.appender.other.maxbackupindex=1000 #输出INFO级别以上的日志 og4j.appender.other.threshold= info #设置使用灵活布局 log4j.appender.other.layout=org.apache.log4j.patternlayout #灵活定义输出格式 Log4j.appender.other.layout.conversionpattern=[%p][%d{yyyy-mm-dd hh:mm:ss}] %l %t %m %n ###################################### # hibernate related Configurations ############################## ######## #输出hibernate调试过程中的错误日志 log4j.logger.org.hibernate=other #输出HQL查询调试日志 Log4j.logger.org.hibernate.hql.ast.ast=other #输出SQL语句调试日志 Log4j.logger.org.hibernate.sql=other #输出 log of &NBSP;JDBC parameter queries log4j.logger.org.hibernate.type=other #输出缓存日志 log4j.logger.org.hibernate.cache=other #输出事务日志 log4j.logger.org.hibernate.transaction=other #输出获取JDBC资源日志 Log4j.logger.org.hibernate.jdbc=other