log4j log to different file configuration analysis by level

Source: Internet
Author: User
Tags log4j

About log4j According to the level of output log, and according to the level of output to different files in the saying there are many, online paste the most log4j.properties settings is such

Log4j.rootlogger=info,stdout,info,debug,error Log4j.appender.stdout=org.apache.log4j.consoleappender Log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=[%-
5p] [%d{hh:mm:ss}]%c-%m%n log4j.logger.info=info Log4j.appender.info=org.apache.log4j.dailyrollingfileappender Log4j.appender.info.layout=org.apache.log4j.patternlayout log4j.appender.info.layout.conversionpattern=[%-5p] [% D{HH:MM:SS}]%c-%m%n log4j.appender.info.datepattern= '. ' Yyyy-mm-dd log4j.appender.info.Threshold = info log4j.appender.info.append=true log4j.appender.info.file=${ Webapp.root}/web-inf/logs/info.log Log4j.logger.debug=debug log4j.appender.debug=
Org.apache.log4j.DailyRollingFileAppender Log4j.appender.debug.layout=org.apache.log4j.patternlayout LOG4J.APPENDER.DEBUG.LAYOUT.CONVERSIONPATTERN=[%-5P] [%d{hh:mm:ss}]%c-%m%n log4j.appender.debug.datepattern= '. ' Yyyy-mm-dd log4j.appender.debug.Threshold = Debug Log4j.appender.debug.append=true Log4j.appendeR.debug.file=${webapp.root}/web-inf/logs/debug.log Log4j.logger.error=error log4j.appender.error=
Org.apache.log4j.DailyRollingFileAppender Log4j.appender.error.layout=org.apache.log4j.patternlayout LOG4J.APPENDER.ERROR.LAYOUT.CONVERSIONPATTERN=[%-5P] [%d{hh:mm:ss}]%c-%m%n log4j.appender.error.datepattern= '. ' Yyyy-mm-dd log4j.appender.error.Threshold = Error Log4j.appender.error.append=true log4j.appender.error.file=${ 

 Webapp.root}/web-inf/logs/error.log


In fact, this part of the code does not work according to log4j level file for file output.

The key configuration instructions in the configuration are this sentence:

Log4j.appender.debug.Threshold = INFO


And its function is to output the content above the info level to ${webapp.root}/web-inf/logs/info.log, so the Info.log file contains the error level files.

The correct solution is:

Define your own Appender class, inherit Dailyrollingfileappender, and overwrite the setting instructions for threshold.

Source Code Record:

  public boolean isassevereasthreshold (Priority Priority)
    {return
        threshold = = NULL | | Priority.isgreaterorequal (threshold);
    }


Overriding the Isassevereasthreshold (Priority Priority) method

public class Logappender extends Dailyrollingfileappender {
 
 @Override public
 boolean isassevereasthreshold ( Priority Priority) {
  ///only to determine if equality is not judged by priority return
  This.getthreshold (). Equals (Priority);
 }



In this way, the only judgment, only when the threshold and priority in line with the output, the realization of the true log4j output log files according to the level.

In Log4j.properties, the configuration file is modified as follows:


Log4j.logger.info=info
Log4j.appender.info=com.company.logappender
log4j.appender.info.layout= Org.apache.log4j.PatternLayout
log4j.appender.info.layout.conversionpattern=[%-5p] [%d{hh:mm:ss}]%c-%m%n
log4j.appender.info.datepattern= '. ' YYYY-MM-DD
log4j.appender.info.Threshold = info 
log4j.appender.info.append=true
Log4j.appender.info.file=${webapp.root}/web-inf/logs/info.log



This completes the ability to output to different files according to the log4j log level.


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.