The Java custom log output file of log4j outputs multiple custom log files

  • 2020-04-01 02:45:00
  • OfStack

Log4j outputs multiple custom log files

If you need to output a separate log file in practice, how can you separate the required content from the original log to form a separate log file?

Start with a common log4j.properties file, which logs in the console and test.log files:


log4j.rootLogger=DEBUG, stdout, logfile  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n  
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=log/test.log
log4j.appender.logfile.MaxFileSize=128MB
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n

What if you need to output multiple log files in the same class? In fact, the principle is the same, first in test.java definition:


private static Log logger1 = LogFactory.getLog("mylogger1");
private static Log logger2 = LogFactory.getLog("mylogger2");


The configuration in log4j.properties is as follows:


log4j.logger.mylogger1=DEBUG,test1
log4j.appender.test1=org.apache.log4j.FileAppender
log4j.appender.test1.File=log/test1.log
log4j.appender.test1.layout=org.apache.log4j.PatternLayout
log4j.appender.test1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n 
log4j.logger.mylogger2=DEBUG,test2
log4j.appender.test2=org.apache.log4j.FileAppender
log4j.appender.test2.File=log/test2.log
log4j.appender.test2.layout=org.apache.log4j.PatternLayout
log4j.appender.test2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n

Logger is used differently for different logs (logger1.info(" ABC ") for the output to test1.log, for example).

Another problem is that these custom logs are output by default to the logs configured by log4j.rootlogger. How can you just have them output to the logs you specify? Don't worry, there's a switch:

Log4j. Additivity. Mylogger1 = false

This is used to set whether or not the log configured by log4j.rootlogger will be output at the same time. If set to false, it will not be output anywhere else.

However, there is a small drawback to this approach, which is that the class name in the printed log can only be mylogger or mylogger2.

2 dynamically configure the path
If the log path required by the program needs to change constantly, and it is impossible to change the configuration file every time, you can use environment variables to solve this problem.

Log4j is configured as follows:


log4j.rootLogger=DEBUG,INFOLOG,DEBUGLOG 
#info log
log4j.appender.INFOLOG =org.apache.log4j.DailyRollingFileAppender
log4j.appender.INFOLOG.File= ${log.dir}/${log.info.file}
log4j.appender.INFOLOG.DatePattern=.yyyy-MM-dd
log4j.appender.INFOLOG.Threshold=INFO
log4j.appender.INFOLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.INFOLOG.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n 

#debug log
log4j.appender.DEBUGLOG =org.apache.log4j.RollingFileAppender
log4j.appender.DEBUGLOG.File= ${log.dir}/${log.debug.file}
log4j.appender.DEBUGLOG.Threshold=DEBUG
log4j.appender.DEBUGLOG.MaxFileSize=128MB
log4j.appender.DEBUGLOG.MaxBackupIndex=3
log4j.appender.DEBUGLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.DEBUGLOG.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n

At this point, before using log to print the log, you need to use System to define the output path of the log and the environment variable of the file name:


System.setProperty( " log.dir " , logDir);
System.setProperty( " log.info.file " , infoLogFileName);
System.setProperty( " log.debug.file " , debugLogFileName);

Attachment: format meaning of the Pattern parameter

%c outputs the full name of the class to which the log information belongs

%d outputs the date or time of the log time point, the default format is ISO8601, you can also specify the format after, for example: %d{yyy-mm-dd HH: MM :ss}, the output is similar to: 2013-8-19-22:10:28

%f outputs the class name of the class to which the log information belongs

%l outputs where the log event occurs, that is, the statement that outputs the log information is on the line of its class

%m outputs information specified in the code, such as message in log(message)

%n outputs a carriage return newline character, Windows platform is "/r/n", Unix platform is "/n"

%p outputs the priority of DEBUG, INFO, WARN, ERROR, FATAL. If the output is from the debug() call, it is debug, and so on

%r outputs the number of milliseconds since the application was started to output this log information

%t outputs the thread name that produces the log event


Related articles: