There is a lot to say about log4j output logs by level and output to different files according to level, the most log4j.properties settings on the web are like this
Log4j.rootlogger=Info,stdout,info,debug,errorlog4j.appender.stdout=Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=[%-5P] [%d{hh:mm:ss}]%c-%m%N Log4j.logger.info=Infolog4j.appender.info=Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.info.layout=Org.apache.log4j.PatternLayoutlog4j.appender.info.layout.ConversionPattern=[%-5P] [%d{hh:mm:ss}]%c-%m%Nlog4j.appender.info.datePattern= '. ' yyyy-mm-Ddlog4j.appender.info.Threshold=INFO log4j.appender.info.append=trueLog4j.appender.info.File=${webapp.root}/web-inf/logs/Info.log Log4j.logger.debug=Debuglog4j.appender.debug=Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.debug.layout=Org.apache.log4j.PatternLayoutlog4j.appender.debug.layout.ConversionPattern=[%-5P] [%d{hh:mm:ss}]%c-%m%Nlog4j.appender.debug.datePattern= '. ' yyyy-mm-Ddlog4j.appender.debug.Threshold=DEBUG log4j.appender.debug.append=trueLog4j.appender.debug.File=${webapp.root}/web-inf/logs/Debug.loglog4j.logger.error=Errorlog4j.appender.error=Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.error.layout=Org.apache.log4j.PatternLayoutlog4j.appender.error.layout.ConversionPattern=[%-5P] [%d{hh:mm:ss}]%c-%m%Nlog4j.appender.error.datePattern= '. ' yyyy-mm-Ddlog4j.appender.error.Threshold=ERROR log4j.appender.error.append=trueLog4j.appender.error.File=${webapp.root}/web-inf/logs/error.log
In fact, this part of the code does not solve the file output according to log4j level files.
The key configuration description in the configuration is this sentence:
Log4j.appender.debug.Threshold = INFO
The function is to output content above the info level to ${webapp.root}/web-inf/logs/info.log, so the Info.log file contains the error level file.
The correct solution is:
Define your own Appender class, inherit the Dailyrollingfileappender, and rewrite the setup instructions for threshold.
Source Code Records:
Public Boolean Isassevereasthreshold (priority priority) { returnnull | | priority.isgreaterorequal (threshold); }
overriding Isassevereasthreshold (Priority priority) method
Public class extends Dailyrollingfileappender { publicboolean isassevereasthreshold (priority Priority) { // only to determine if it is equal and not to determine the precedence returnthis. Getthreshold (). Equals (priority); }}
In this way, only when the threshold is consistent with the priority, the output is achieved, and the actual log4j is output by the level log file.
In Log4j.properties, make the following modifications to the configuration file:
log4j.logger.info=Infolog4j.appender.info=com.company.LogAppenderlog4j.appender.info.layout= org.apache.log4j.PatternLayoutlog4j.appender.info.layout.ConversionPattern=[%-5p] [%d{hh:mm:ss}]%c-%m% nlog4j.appender.info.datePattern= '. ' yyyy-mm-= INFO log4j.appender.info.append=truelog4j.appender.info.File=${ Webapp.root}/web-inf/logs/info.log
This completes the ability to output to different files according to the log level in log4j.
This article was transferred from this author address: http://blog.csdn.net/projava/article/details/7344839
"Go" log4j output logs to different file configuration analysis by level