Detailed explanation of log4j configuration file ----- self study

Keywords: Java Linux perl

Let's start with a configuration file----

log4j.rootLogger=debug,stdout,logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=d:/msb.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd   HH:mm:ss} %l %F %p %m%n

Requirements beyond log content----
1. The configuration file format of log4j is. properties, which must be named
log4j.properties otherwise, reading the configuration file fails;

2. In the standard format, key value pairs are used to save the configuration content - key=value;

3. It is customary to put the log4j.properties file in the root directory, because by default, searching for the log configuration file is to find the file named log4j.properties in CLASSPATH.

Log content requirements------
Log4j. Rootlogger ------ > indicates the level and method of logging

Log4j. Rootlogger = debug, stdout, logfile ------ > the logging level is debug, and the output methods are stdout and logfile (the two methods need to be specified by yourself)

Logging level - priority of

  1. fatal: very serious error events have occurred, which may cause the program to terminate abnormally

  2. error: Although there are errors, the application is allowed to continue running

  3. warn: hidden hazards in the operating environment

  4. info: report information

  5. debug: fine-grained information events, corresponding to program debugging;

Each level includes the level above him;
That is, the output level error includes fatal, and debug includes all the above levels

log4j.appender.stdout = -------- indicates the output mode

Log4j. Appender. Stdout = org. Apache. Log4j. Consoleappender -------- > output on console
Log4j. Appender. Stdout. Target = system. Err --- > > > indicates that the output information is system error
Log4j. Appender. Stdout. Layout = org. Apache. Log4j. Simplelayout ----- > > indicates that the output format is simple

The same is understandable----
Log4j. Appender. Logfile = org. Apache. Log4j. Fileappender - > > the output location is in the log file
Log4j. Appender. Logfile. File = D: / MSB. Log ----- > > specify the log file location
Log4j. Appender. Logfile. Layout = org. Apache. Log4j. Patternlayout ----- > > > output format ----- specify format
Log4j. Appender. Logfile. Layout. Conversionpattern =% d{yyyy MM DD HH: mm: SS}% l% F% P% m% n ----- > > > define the specified format

log4j.properties syntax:

##Define the root logger with appender X
log4j.rootLogger = DEBUG, X
##Set the appender named X to be a File appender
log4j.appender.X=org.apache.log4j.FileAppender
##Define the layout for X appender
log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.conversionPattern=%m%n

In log4j.appender.X., as long as X is consistent, stdout and logfile are not necessarily used, but it is best to see the name and meaning;

However, in daily use, you generally do not write it yourself, but directly go to the official website to find the relevant configuration file, copy and paste it, and then change it to use it yourself;
Log the output of the log4j.properties configuration file to the console.

log4j.rootLogger=debug,stdout,logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

Log the output of the log4j.properties configuration file to the log file.

log4j.rootLogger=debug,logfile
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=d:/msb.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd   HH:mm:ss} %l %F %p %m%n

It can also be recorded at the same time, but it is generally required on demand. Users do not need to see some error messages, but only give users what they should see;

After touching xml again, you can also use xml to fetch and complete the configuration file-----

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
  xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" 
      value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
      </layout>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="console" />
  </root>
</log4j:configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
  xmlns:log4j="http://jakarta.apache.org/log4j/">
 
  <appender name="file" class="org.apache.log4j.RollingFileAppender">
     <param name="append" value="false" />
     <param name="maxFileSize" value="10KB" />
     <param name="maxBackupIndex" value="5" />
     <!-- For Tomcat -->
     <param name="file" value="${catalina.home}/logs/my.log" />
     <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" 
      value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
     </layout>
  </appender>
 
  <root>
    <level value="ERROR" />
    <appender-ref ref="file" />
  </root>
 
</log4j:configuration>

Finally, integrate a standard

# Root logger option
log4j.rootLogger=INFO, file, stdout
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\my.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
# Direct log messages to stdout
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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
  xmlns:log4j="http://jakarta.apache.org/log4j/">
 
  <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" 
      value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
      </layout>
  </appender>
 
  <appender name="file" class="org.apache.log4j.RollingFileAppender">
      <param name="append" value="false" />
      <param name="maxFileSize" value="10MB" />
      <param name="maxBackupIndex" value="10" />
      <param name="file" value="${catalina.home}/logs/my.log" />
      <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" 
      value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
      </layout>
  </appender>
 
  <root>
    <level value="DEBUG" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
  </root>
 
</log4j:configuration>

Logging method------

package com.gavin;
import org.apache.log4j.Logger;
import java.io.IOException;
public class Test01 {
    public static void main(String[] args) throws IOException {
        //Logger logger = Logger.getLogger("com.gavin.Test01");// The full pathname of the class
        Logger logger = Logger.getLogger(Test01.class);
    
       logger.fatal("fatal");
        logger.error("error");
        logger.warn("warn");
        logger.info("info");
        //logger.debug("debug");
        try {
            int result = 1 / 0;
        } catch (Exception e) {
            logger.error("Program operation error", e);
        }
    }
}

Note ----- the above contents are for reference only;

Posted by bpops on Thu, 18 Nov 2021 21:06:48 -0800