Configure Log4j (very detailed)

[From http://www.blogjava.net/zJun/archive/2006/06/28/55511.html]

Log4J's Configuration File is used to set the level, register and layout of the recorder. It can receive key=value format settings or xml format settings. By configuring, you can create the running environment of Log4J.

  1. configuration file
    The basic format of Log4J configuration file is as follows:
#Configure the root Logger
log4j.rootLogger  =   [ level ]   ,  appenderName1 ,  appenderName2 ,  ...

#Configure the log information output destination Appender
log4j.appender.appenderName  =  fully.qualified.name.of.appender.class 
  log4j.appender.appenderName.option1  =  value1 
  ... 
  log4j.appender.appenderName.optionN  =  valueN 

#Configure the format (layout) of log information
log4j.appender.appenderName.layout  =  fully.qualified.name.of.layout.class 
  log4j.appender.appenderName.layout.option1  =  value1 
  ... 
  log4j.appender.appenderName.layout.optionN  =  valueN 

Among them, [level] is the log output level, which has five levels:

FATAL       0  
ERROR      3  
WARN       4  
INFO         6  
DEBUG      7

appender is the destination of log output. Log4j provides several appenders:

org.apache.log4j.ConsoleAppender (console)
org.apache.log4j.FileAppender (file)
Org. apache. log4j. Daily Rolling File Appender (produces a log file every day)
org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size)
org.apache.log4j.WriterAppender (sending log information in streaming format to any designated place)

Layout: Log output format. Log4j provides layouts as follows:

org.apache.log4j.HTMLLayout (laid out in HTML tables)
org.apache.log4j.PatternLayout (you can specify layout patterns flexibly)
org.apache.log4j.SimpleLayout (level and information string containing log information)
org.apache.log4j.TTCCLayout (including log generation time, threads, categories, etc.)

Print parameters: Log4J formats log information with print format similar to the printf function in C language, as follows:

 % m The message specified in the output code
 % p Output Priority, namely DEBUG, INFO, WARN, ERROR, FATAL 
% r Number of milliseconds taken to output log information from application startup 
% c Outputs the full name of the class to which it belongs. 
% t outputs the thread name that produces the log event 
% n outputs a return line break, Windows platform is "/r/n", Unix platform is "/n" 
% d outputs the date or time of the log time point, the default format is ISO8601, or you can specify the format later, such as:% d {yyy MMM DD HH: mm: ss, SSS}, the output is similar: October 18, 2002 22:10:28, 921  
% l Output the location of the log event, including the class name, the thread that occurred, and the number of lines in the code. Examples: Testlog4.main(TestLog4.java: 10)
  1. Initialize Logger in code:
    1) Call the Basic Configurator. configure () method in the program: add a Console Appender to the root recorder, and set the output format to "%-4r [% t]% -5p% c% X -% m% n" through PatternLayout, and the default level of the root recorder is Level.DEBUG.
    2) The configuration is placed in the file, the file name is passed through the command line parameters, and parsed and configured by Property Configurator. configure (args [x]).
    3) Configuration is placed in the file, and information such as file name is passed through environment variables, which is parsed and configured by the default initialization process of log4j.
    4) Configuration is placed in the file. The file name and other information are transmitted through the application server configuration, and a special servlet is used to complete the configuration.

  2. Set the log output level for different Appender s:
    When debugging the system, we often pay attention to the log output of exception level, but usually all levels of output are placed in a file, if the level of log output is BUG!? Let's go and find it slowly.
    At this point, we may want to be able to output exceptional information to a single file. Of course, Log4j already provides this functionality, and we only need to modify the Threshold of Appender in the configuration. For example, the following example:

[Configuration file]

### set log levels ###
log4j.rootLogger = debug ,  stdout ,  D ,  E

### Output to console ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n

### Output to log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG ## Output logs above DEBUG level
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### Save exception information to a separate file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log ## Exception log file name
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## Only output logs above ERROR level!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

[Use in code]

 public   class  TestLog4j  {
     public   static   void  main(String[] args)  {
        PropertyConfigurator.configure( " D:/Code/conf/log4j.properties " );
        Logger logger  =  Logger.getLogger(TestLog4j. class );
        logger.debug( " debug " );
        logger.error( " error " );
    } 
}

Attach the usual configuration in the project
log4j.properties

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] %c{1}:%L - %m%n


log4j.rootLogger=warn, stdout, R

log4j.logger.com.yun=debug

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=E:/yun/yun.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %5p  %c - %m%n

Keywords: log4j Apache xml Windows

Added by drayarms on Fri, 21 Jun 2019 23:40:35 +0300