This section describes the log configuration of log4j2 for subsequent learning

1. Post a complete configuration and study it (O (////, ////) Q posted it from someone else) Original: www.cnblogs.com/hafiz/p/617…

<? xml version="1.0" encoding="UTF-8"? > <! -- Log levels and priorities: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL --> <! -- Status after Configuration, this is used for Settingslog4j2's own internal output, you don't have to set it, but when you set it to trace, you'll see thatlog4j2 internal various verbose output --> <! --monitorInterval: Log4j automatically detects changes to configuration files and reconfigures itself, sets the interval in seconds --> < Configuration Status ="WARN" monitorInterval="30"> <! Define all appenders first --> <appenders> <! -- This output console configuration --> <console name="Console" target="SYSTEM_OUT"> <! PatternLayout Pattern = --> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> </console> <! -- The file will print out all the information, thislogThe application is automatically cleared each time it is run, as determined by the append property. This is also useful for temporary testing --> <File name="log" fileName="log/test.log" append="false">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </File> <! <RollingFile name= > <RollingFile name= > <RollingFile name= > <RollingFile name= > <RollingFile name= > <RollingFile name= > <RollingFile name= > <RollingFile name= > <RollingFile name= > <RollingFile name= > <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log"
                      filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log"> <! -- The console outputs only messages of level or higher (onMatch). The other messages are rejected (onMismatch) --> <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
             <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="100 MB"/>
             </Policies>
         </RollingFile>
         <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/warn.log"
                      filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
             <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
             <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> <! -- DefaultRolloverStrategy Max = 20 --> <DefaultRolloverStrategy Max ="20"/>
         </RollingFile>
         <RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/error.log"
                      filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
             <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
             <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> </RollingFile> </appenders> <! Then define logger. The appenders will only work if logger is defined and appenders are introduced --> <loggers> <! -- Filter out spring and Mybatis DEBUG messages --> < Logger name="org.springframework" level="INFO"></logger>
         <logger name="org.mybatis" level="INFO"></logger>
         <root level="all">
             <appender-ref ref="Console"/>
             <appender-ref ref="RollingFileInfo"/>
             <appender-ref ref="RollingFileWarn"/>
             <appender-ref ref="RollingFileError"/>
         </root>
     </loggers>
 </configuration>
Copy the code

2, about the name of the configuration file and its location in the project

Log4j 2.x no longer supports file configuration with the. Properties suffix in 1.x, and the configuration file name extension in 2.x can only be “.xml”,”.json” or “.jsn”.

The system selects the following configuration file priorities (from first to last) :

Json or log4j2-test. JSN in.classpath.

Classpath log4j2-test.xml

(3).classpath log4j2.json or log4j2. JSN

(4). Under the classpath is called log4j2. XML files. 3, node configuration file parsing

   

The root node Configuration has two attributes: Status and MonitorInterval, and two child nodes :Appenders and Loggers(indicating that multiple Appenders and Loggers can be defined). Status is specifiedlogThe level of logging that 4j itself prints. Monitorinterval Specifieslog4j automatically reconfigures the monitoring interval in s with a minimum of 5s. (2).Appenders node, Name: specifies the name of the Appender target:SYSTEM_OUT or Appender SYSTEM_ERR, generally set the default: system_out. PatternLayout: output format, The default value is no. %m%n. File The object defines the appender. name: specifies the name of the Appender File fileName: specifies the name of the Appender File with the full path to the destination log File. PatternLayout: Output format, No Default :%m% N. RollingFile This object is used to define how to automatically delete old appenders. create new appenders. name: specifies the name of the Appender. PatternLayout: Output format, not set Default :%m% N. filePattern: Specifies the name format of the new log file. Policies: Specifies the rolling log policy, that is, when to create a log file and export logs. TimeBasedTriggeringPolicy: Policies child nodes, rolling strategy based on time, the interval attribute is used to specify how long scroll, the default is 1 hour. modulate=trueFor example, if it's 3am in the morning and interval is 4, the first scroll will be at 4am, then 8am, then 12am... Rather than 7 am. SizeBasedTriggeringPolicy: Policies child nodes, rolling strategy based on the specified file size, the size attribute is used to define the size of each log file. DefaultRolloverStrategy: Used to specify the maximum number of log files in a folder to start deleting the oldest and creating new ones (via the Max attribute). The Root node is used to specify the Root log of the project. If Logger is not specified separately, the Root log output is used by default. All < Trace < Debug < Info < Warn < Error < Fatal < off.appenderref: The Root appender. Logger node is used to specify the log format separately, for example, to specify different log levels for classes under specified packages. Level: indicates the log output level. There are eight log output levels in ascending order: All < Trace < Debug < Info < Warn < Error < Fatal < off. name: specifies the class to which the Logger belongs or the package path to which the class belongs, inherited from the Root node. AppenderRef: Logger's child node that specifies to which Appender the log should be exported. If this is not specified, it inherits from Root by default. If specified, it will output in both the specified Appender and Root's Appender, and we can set Logger additivity="false"Output is only done in a custom Appender. There are eight log levels, from lowest to highest: All < Trace < Debug < Info < Warn < Error < Fatal < OFF. All: the lowest level, which is used to enable All logging. Trace: it's Trace, it's Trace, it's Trace, it's Trace, it's Trace, it's Trace, it's Trace, it's Trace, it's Trace, it's Trace, it's Trace, it's Trace, it's Trace. Debug: Indicates fine-grained information events that are very helpful for debugging applications. Info: Messages highlight the execution of the application at a coarse-grained level. Warn: Logs of warning and lower levels are generated. Error: Logs of Error information are output. Fatal: logs every critical error event that will cause the application to exit. OFF: indicates the highest level. It is used to disable all logging. The program prints logs at or higher than the specified log level. The higher the log level is, the less logs are printed.Copy the code

${sys:user.home} : Obtain the home directory of the user

Post a YML format, simpler than the above configuration, but should be enough to use, their own use of this

Configuration:
  status: warn

  Properties: Define global variables
    Property: Default configuration (for the development environment). Other environments need to be specified in VM parameters as follows:
      Console =warn - dlog.level. XJJ =trace
      Console =warn - dlog.level. XJJ =info
      - name: log.level.console
        value: debug
      - name: log.level.xjj
        value: info
      - name: log.path
        value: D:/log
      - name: project.name
        value: springboot1

  Appenders:
    Console:  Output to the console
      name: CONSOLE
      target: SYSTEM_OUT
      ThresholdFilter:
        level: ${sys:log.level.console} # "sys:" means: if this variable value is not specified in the VM parameter, the default global variable value defined in this file is used
        onMatch: ACCEPT
        onMismatch: DENY
      PatternLayout:
        pattern: "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"
    RollingFile: # Output to file, over 100MB archive
      - name: ROLLING_FILE
        ignoreExceptions: false
        fileName: ${log.path}/${project.name}.log
        filePattern: "${log.path}/ $${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
        PatternLayout:
          pattern: "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: "100 MB"
        DefaultRolloverStrategy:
          max: 1000

  Loggers:
    Root:
      level: info
      AppenderRef:
        - ref: CONSOLE
        - ref: ROLLING_FILE
    Logger: Configure a special Log level for org.spring Framework packages to facilitate debugging
      - name: org.springframework
        additivity: false
        level: ${sys:log.level.xjj}
        AppenderRef:
          - ref: CONSOLE
          - ref: ROLLING_FILE

Copy the code