Make log4j different level logs output to different files

Source: Internet
Author: User
Tags log4j
 For a large system, especially a high-volume web system, it is not easy to check for important error or fatal information in a log file that records a vast amount of info, even debug log information, even if you use grep, which is quite passive. If we can configure a different file Appender for the same logger to record all the information (generally on-line system is info level), another problem that only records the error above is solved. Log4j have such control properties, not common, but there are ^_^:
Log4j.appender.errorfileout.Threshold = ERROR
XML format configuration (placed in the appender tag): <param name= "Threshold" value= "ERROR"/>

In this way, the error and fatal level information is recorded in the Errorfileout Appender. A more complete example of a configuration file is given below:
Log4j.rootlogger=debug,stdout
Log4j.logger.net.dxtop=debug,fileout,errorfileout
#log4j. Additivity.net.dxtop=false

#stdout Appender
Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
log4j.appender.stdout.layout.conversionpattern=%d (%f:%l)%-5p%c-%m%n

#common file Appender
Log4j.appender.fileout=org.apache.log4j.rollingfileappender
Log4j.appender.fileout.file=d:/logs/sys.log
Log4j.appender.fileout.layout=org.apache.log4j.patternlayout
log4j.appender.fileout.layout.conversionpattern=%d [%t] (%f:%l)%-5p%c-%m%n
log4j.appender.fileout.maxfilesize=100000kb
log4j.appender.fileout.maxbackupindex=200

#error file Appender
Log4j.appender.errorfileout=org.apache.log4j.rollingfileappender
Log4j.appender.errorfileout.file=d:/logs/sys_error.log
Log4j.appender.errorfileout.layout=org.apache.log4j.patternlayout
log4j.appender.errorfileout.layout.conversionpattern=%d [%t] (%f:%l)%-5p%c-%m%n
log4j.appender.errorfileout.maxfilesize=100000kb
Log4j.appender.errorfileout.Threshold = ERROR
log4j.appender.errorfileout.maxbackupindex=200

If you do not want the error and fatal to be put together, you can refer to the reference document listed later in this article, for most people, the above configuration method is sufficient.

Another less common attribute, additivity, is also useful, see the file above: #log4j. additivity.net.dxtop=false
If the value is false, the information output by net.dxtop This logger Appender is not written to the Appender of its parent logger.

Reference Documentation:
Http://jackgm.spaces.live.com/blog/cns!9722965D743EDAF9!774.entry
Http://www.daniweb.com/forums/thread109937.html

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.