Previously wrote a post, is log4j by the package path output to different files.
log4j output to different files by level, also similar.
First look at the configuration:
### set log Levels ### Log4j.rootlogger=info,error,info log4j.appender.stdout=org.apache.log4j.consoleappender log4j. Appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=%d{ YYYY-MM-DD-HH-MM} [%t] [%c] [%p]-%m%n log4j.logger.info=info log4j.appender.info=com.zznode.log.myappender Log 4j.appender.info.layout=org.apache.log4j.patternlayout log4j.appender.info.layout.conversionpattern=%d{ YYYY-MM-DD-HH-MM} [%t] [%c] [%p]-%m%n log4j.appender.info.datepattern= '. ' Yyyy-mm-dd log4j.appender.info.Threshold = info Log4j.appender.info.append=false log4j.appender.info.file=d:/log4j/ Info.log Log4j.logger.error=error Log4j.appender.error=com.zznode.log.myappender log4j.appender.error.layout= Org.apache.log4j.PatternLayout log4j.appender.error.layout.conversionpattern=%d{yyyy-mm-dd-hh-mm} [%t] [%c] [%p]-% m%n log4j.appender.error.datepattern= '. ' Yyyy-mm-dd log4j.appender.error.Threshold = Error Log4j.appender.erRor.append=false Log4j.appender.error.file=d:/log4j/error.log
Test class:
Import Org.apache.log4j.PropertyConfigurator;
Import Org.apache.log4j.xml.DOMConfigurator;
Import Com.zznode.test.Test;
/**
* @desc:
* @since Nov 8, *
@author Chaisson *
* <p> public
class Log4japp { Public
void Printlog () {
Logger log = Logger.getlogger (Log4jApp.class.getClass ());
Log.info ("Test Info");
Log.debug ("Test Debug");
Log.error ("Test Error");
}
public static void Main (string[] args) {
//domconfigurator.configure ("Log4j.xml");
Propertyconfigurator.configure ("D:/workspace/test/log4j-new.properties");
Log4japp app = new Log4japp ();
App.printlog ();
Test test = new test ();
Test.printlog ();
}
There is a problem with the output: The Info.log contains the output of the error.
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 Info.log, so the Info.log file contains the error level files.
The workaround is to define your own Appender class, inherit the Dailyrollingfileappender, and overwrite the setting instructions for threshold (overriding the comparison method for the level)
Source
public boolean isassevereasthreshold (Priority Priority) {return
threshold = = NULL | | Priority.isgreaterorequal (threshold);
}
Overriding the Isassevereasthreshold (Priority Priority) method
/**
* @desc:
* @since APR, 2013
* @author Chaisson *
* <p> public
class Myappender extends Dailyrollingfileappender {
@Override public
boolean isassevereasthreshold (Priority Priority) {
///Only if the equality is judged, not the 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.
To modify a configuration file:
### set log Levels ### Log4j.rootlogger=info,error,info log4j.appender.stdout=org.apache.log4j.consoleappender log4j. Appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=%d{ YYYY-MM-DD-HH-MM} [%t] [%c] [%p]-%m%n log4j.logger.info=info log4j.appender.info=com.zznode.log.myappender Log 4j.appender.info.layout=org.apache.log4j.patternlayout log4j.appender.info.layout.conversionpattern=%d{ YYYY-MM-DD-HH-MM} [%t] [%c] [%p]-%m%n log4j.appender.info.datepattern= '. ' Yyyy-mm-dd log4j.appender.info.Threshold = info Log4j.appender.info.append=false log4j.appender.info.file=d:/log4j/ Info.log Log4j.logger.error=error Log4j.appender.error=com.zznode.log.myappender log4j.appender.error.layout= Org.apache.log4j.PatternLayout log4j.appender.error.layout.conversionpattern=%d{yyyy-mm-dd-hh-mm} [%t] [%c] [%p]-% m%n log4j.appender.error.datepattern= '. ' Yyyy-mm-dd log4j.appender.error.Threshold = Error Log4j.appender.erRor.append=false Log4j.appender.error.file=d:/log4j/error.log
That's the end of it.
Add: Some colleagues discuss that if it is an XML configuration, the filter will be simple:
<filter class= "Org.apache.log4j.varia.LevelRangeFilter" >
<param name= "levelmin" value= "ERROR"/>
<param name= "Levelmax" value= "ERROR"/>
</filter>
Interested friends, you can try their own hands.