Java log output file custom

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Java log output file custom

Log print, in the program is essential, if need to combine different log printing to different places, you need to define different Appender, then the definition of each Appender log level, the output path in printed form and log.

1 log4j output multiple custom log file

If you need to output in the practical application of the log file, how to get the required content is separated from the original log, forming a single log file.?

Look at a common log4j.properties file, it is logged in the console and the test.log file:  

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

  If you need to output a plurality of log files in the same class? In fact, the truth is the same, first defined in the Test.java:

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

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

  The log4j.properties configuration is as follows:

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

Different log to use a different logger (such as output to test1.log for logger1.info("abc")).

There is a problem, it is the custom log default is also output to the log4j.rootLogger configuration in the log, how can just let their output to the specified log in? Don't worry, there is a switch:

log4j.additivity. mylogger1 = false

It is also used to set the output to the log4j.rootLogger configuration in the log, set to false will not be output to other places.

But this way there is a small defect, the class name is printed in the log is only mylogger or mylogger2.

2 dynamic configuration path

If the log path program requires the need to constantly change, and it is not possible to change the configuration file every time, at this time can be solved by using environment variables.

The log4j configuration is as follows:

 1 log4j.rootLogger=DEBUG,INFOLOG,DEBUGLOG 
 2 
 3 #info log
 4 log4j.appender.INFOLOG =org.apache.log4j.DailyRollingFileAppender
 5 log4j.appender.INFOLOG.File= ${log.dir}/${log.info.file}
 6 log4j.appender.INFOLOG.DatePattern=.yyyy-MM-dd
 7 log4j.appender.INFOLOG.Threshold=INFO
 8 log4j.appender.INFOLOG.layout=org.apache.log4j.PatternLayout
 9 log4j.appender.INFOLOG.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n 
11  
12 #debug log
13 log4j.appender.DEBUGLOG =org.apache.log4j.RollingFileAppender
14 log4j.appender.DEBUGLOG.File= ${log.dir}/${log.debug.file}
15 log4j.appender.DEBUGLOG.Threshold=DEBUG
16 log4j.appender.DEBUGLOG.MaxFileSize=128MB
17 log4j.appender.DEBUGLOG.MaxBackupIndex=3
18 log4j.appender.DEBUGLOG.layout=org.apache.log4j.PatternLayout
19 log4j.appender.DEBUGLOG.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n

At this time, before using the log print log, need to use the output path and file System defines the log name environment variables:

   System.setProperty(“log.dir”, logDir);

   System.setProperty(“log.info.file”, infoLogFileName);

   System.setProperty(“log.debug.file”, debugLogFileName);

Attached: format meaning of Pattern parameters

%c The output log information class name

%d Output log at the time of the date or time, the default format for the ISO8601, also can be in after the specified format, such as:%d{yyy-MM-dd  HH:mm:ss }, similar to the output: 2013-8-19- 22: 10: 28

%f The output log information class

%l The position output log events, namely the output log information statement in its class line number

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

%n The output of a carriage returns, Windows platform for “ /r/n”, Unix platform“/n”

%p Output priority, i.e. DEBUG, INFO, WARN, ERROR, FATAL. If it is to call debug () output, is DEBUG, and so on

%r Output from the application start to the output of the log information consumes the number of milliseconds

%t Output the log event thread name


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by David at November 13, 2013 - 3:12 PM