Some experiences of using log4j in spring

Source: Internet
Author: User

Spring uses log4j in two ways.

1. No configuration is made in Web. xml.

Log4j. properties is stored in the root directory of classpath,

At this time, the generated log file does not have a relative path. if you write a relative path, it is generated under the root path of Tomcat installation.

2. Set in Web. xml.

<context-param><param-name>log4jConfigLocation</param-name><param-value>WEB-INF/classes/log4j.properties</param-value></context-param><context-param><param-name>webAppRootKey</param-name><param-value>myappfuse.root</param-value></context-param><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

Note that:

1) It must be written as follows:

<param-value>WEB-INF/classes/log4j.properties</param-value>

If it is written:

<param-value>classpath*:log4j.properties</param-value>

The file cannot be found.

2) It must be written as follows:

<param-name>log4jConfigLocation</param-name>

3)
Log4jconfiglistener must be defined in
Before contextloaderlistener, otherwise two log files will appear, one in the project path and the other in the Tomcat root path.
4) if not defined
<context-param><param-name>webAppRootKey</param-name><param-value>myappfuse.root</param-value></context-param>
The path in the log4j. properties file should be: # {webapp. Root} by default}
3. Different Levels are displayed in different files:
#log4j.logger.com.ibatis=DEBUGlog4j.logger.com.ibatis=OFFlog4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUGlog4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUGlog4j.logger.com.ibatis.SQL Map.engine.impl.SQL MapClientDelegate=DEBUG#log4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Connection=OFFlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUGlog4j.logger.java.sql.ResultSet=DEBUG#log4j.appender.R.File=${webapp.root}/WEB-INF/logs/myfuse.log #log4j.rootLogger=INFO,R#log4j.rootLogger=FATAL,Rlog4j.rootLogger=INFO,R,stdout,Elog4j.addivity.org.apache=truelog4j.appender.R=org.apache.log4j.DailyRollingFileAppender#log4j.appender.R.File=${webapp.root}/WEB-INF/logs/chplan_info1.loglog4j.appender.R.File=${myappfuse.root}/www/log/chplan_info.log#log4j.appender.R.DatePattern=yyyy-MM-dd'.log'# Keep one backup file  log4j.appender.R.layout=org.apache.log4j.PatternLayout#log4j.appender.R.layout.ConversionPattern= %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%nlog4j.appender.R.layout.ConversionPattern= %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p]   %m%nlog4j.logger.E=error log4j.appender.E=org.apache.log4j.DailyRollingFileAppender log4j.appender.E.layout=org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern=[%p] [%d{yyyy-MM-dd HH:mm:ss}]  [ Class = %C | Method = %M | Line = %L ] | %m |%n log4j.appender.E.datePattern='.'yyyy-MM-dd log4j.appender.E.Threshold=ERROR log4j.appender.E.append=true #log4j.appender.E.Encoding=GBK #log4j.appender.E.File=${webapp.root}/WEB-INF/logs/chplan_error.log log4j.appender.E.File=${myappfuse.root}/www/log/chplan_error.loglog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.