log4j outputs multiple custom log files
If you need to output independent log files in actual applications, how can you separate the required content from the original log and form a separate log file?
Let’s first look at a common log4j.properties file, which records logs in the console and test.log files:
The code copy is as follows:
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 multiple log files need to be output in the same class? In fact, the truth is the same. First, it is defined in Test.java:
The code copy is as follows:
private static Log logger1 = LogFactory.getLog("mylogger1");
private static Log logger2 = LogFactory.getLog("mylogger2");
The configuration in log4j.properties is as follows:
The code copy 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
Different logs should be used for different logs (for example, logger1.info("abc") should be used for outputting to test1.log).
Another problem is that these custom logs are output to the logs configured by log4j.rootLogger by default. How can you only output them to the logs specified by yourself? Don't worry, here is a switch:
log4j.additivity. mylogger1 = false
It is used to set whether to output it to the log configured by log4j.rootLogger at the same time. If it is set to false, it will not output to other places.
However, this method has a small flaw, that is, the class name in the printed log can only be mylogger or mylogger2.
2 Dynamic configuration paths
If the log path required by the program needs to be constantly changed, and it is impossible to change the configuration file every time, you can use environment variables to solve it.
The configuration of log4j is as follows:
The code copy is 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 time, before using log to print the log, you need to use System to define the output path and file name environment variables:
The code copy is as follows:
System.setProperty("log.dir", logDir);
System.setProperty("log.info.file", infoLogFileName);
System.setProperty("log.debug.file", debugLogFileName);
Attachment: The format meaning of the Pattern parameter
%c 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 afterwards, such as: %d{yyy-MM-dd HH:mm:ss }, and the output is similar: 2013-8-19- 22:10:28
%f The class name of the class to which the log information belongs
%l The output log event occurs, that is, the statement that outputs log information is in which row of the class it is located
%m Output the information specified in the code, such as message in log(message)
%n outputs a carriage return line break, Windows platform is "/r/n", and Unix platform is "/n"
%p output priority, i.e. DEBUG, INFO, WARN, ERROR, FATAL. If it is output by calling debug(), it is DEBUG, and so on
%r Output the number of milliseconds taken from the start of the application to output the log information
%t outputs the thread name that generates the log event