log4j2 Configuration Introduction

This blog is republished from Ahao dry talk of " chat log4j2 profile log4j2.xml ", Bowen address: https: //www.cnblogs.com/hafiz/p/6170702.html

Recently started To learn more about configuration log4j2 to find such a comprehensive introduction on the Internet, thanks to " Ahao chat dry " share

Or less share blog content.

One, background

 Yesterday understand what slf4j and log4j2, want to summarize the record, very simple configuration steps, the only configuration required is log4j2.xml specific knowledge of, so to find this blog, explaining in great detail the configuration, then reproduced for the record, thanks again Ahao chat dry. The following text:  

  Recently due to the need of the project, we log4j 1.x versions have been migrated to log4j 2.x version, and that the ensuing slf4j integrated configuration of log4j ( using Slf4j integrated Log4j2 perfect solution to build the project log system ) and Detailed log4j2 profiles, we need a good talk. In this paper, specifically in terms of the label off his sense of log4j2.xml profile.

Second, the configuration of the whole solution

  1. The storage location of the configuration file name as well as in the project

    log4j 2.x versions are no longer supported image file configuration .properties suffix in 1.x, 2.x version of the configuration file extension can only be ".xml", ". json" or ".jsn".

    Profile system selection priority (oldest first) as follows:

      (1) file name or log4j2-test.json log4j2-test.jsn under .classpath.

      (2) file named log4j2-test.xml under .classpath.

      (3) .classpath under the file name or log4j2.jsn of log4j2.json.

      (4) .classpath under the file named log4j2.xml.

     We generally use log4j2.xml named default. If you want to test locally, you can put log4j2-test.xml into the classpath, and formal environments log4j2.xml, then the deployment package when not packed log4j2-test.xml can be.

  2. The default default profile

Copy the code

 1  <?xml version="1.0" encoding="UTF-8"?>
 2  <Configuration status="WARN">
 3    <Appenders>
 4      <Console name="Console" target="SYSTEM_OUT">
 5        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
 6      </Console>
 7    </Appenders>
 8    <Loggers>
 9      <Root level="error">
10        <AppenderRef ref="Console"/>
11      </Root>
12    </Loggers>
13  </Configuration>

Copy the code

 

  3. The node configuration file parsing    

    (1) Configuration root node has two attributes: status and monitorinterval, there are two child nodes: Appenders and Loggers is (indicate Appender and may define a plurality Logger).

       status is used to specify the level of log4j own print log.

       Monitoring interval monitorinterval log4j specifies automatic reconfiguration unit is s, the minimum is 5s.

    (2) .Appenders node, there are three common seed node: Console, RollingFile, File.

       Console node is used to define the output Appender console.

        name: Name of Appender.

        target: SYSTEM_OUT or SYSTEM_ERR, generally set the default: SYSTEM_OUT.

        PatternLayout: output format is not set by default:% m% n.

       File nodes Appender used to define the location to the specified file.

        name: Name of Appender.

        fileName: specify the output file name of the destination log file with full path.

        PatternLayout: output format is not set by default:% m% n.

       RollingFile node used to define more than the specified size are automatically deleted in the new Appender old creation.

        name: Name of Appender.

        fileName: specify the output file name of the destination log file with full path.

        PatternLayout: output format is not set by default:% m% n.

        filePattern: Specifies the name of the new log file format.

        Policies: Specifies the log rolling strategy is when to create a new log file output log.

        TimeBasedTriggeringPolicy: Policies child node, based on a rolling strategy time, interval attribute is used to specify how often rolling once, the default is 1 hour. modulate = true is used to adjust the time: for example, now is the morning 3 am,interval is 4, then rolling in the first 4am, followed by 8 am,12am ... instead of 7am.

        SizeBasedTriggeringPolicy: Policies child node, based on a rolling policy specifies the file size, size attribute is used to define the size of each log file.

        DefaultRolloverStrategy: used to specify the start deleting the oldest when the same folder with up to several log files, create new (by max attribute).

    (3) .Loggers node, there are two common: Root and Logger.

       Root node to the root log specified item, if no separate Logger, it will use the default log output Root

         level: the log output levels, a total of eight levels, from low to high in accordance with: All <Trace <Debug <Info <Warn <Error <Fatal <OFF.

         AppenderRef: Root child node, which specifies to which log output Appender.

       Logger node specifies a separate log form, for example, to specify a different logging level as the class specified in the packet.

         level: the log output levels, a total of eight levels, from low to high in accordance with: All <Trace <Debug <Info <Warn <Error <Fatal <OFF.

         name: Logger used to specify the class or package applies the full path to where the class inherits from the Root node.

         AppenderRef: Logger child node, is used to specify the log output to Appender which, if not specified, the default will be inherited from the Root If specified, then the output will be specified in Appender this Appender and Root in this case us. Logger may be provided to additivity = "false" is output only from the Appender defined.

    (4) About the log level.

      Eight levels, from low to high in accordance with: All <Trace <Debug <Info <Warn <Error <Fatal <OFF.

      All: the lowest level, to open all logging.

      Trace: a track, is to promote the program, you can write trace output, so the trace should be particularly large, but that's okay, we can not let him set the minimum log level output.

      Debug: fine-grained information about the event pointed to debug an application is very helpful.

      Info: Message in a coarse-grained level to highlight the process of running the application.

      Warn: Warnings and warn the output level of the log.

      Error: error message log output.

      Fatal: The output of each event will lead to serious error log to exit the application.

      OFF: the highest level for close all logging.

      The program will print higher than or equal to the set level log, the log level is set higher, the less printed journal .

  4. Compare the complete configuration template log4j2.xml

Copy the code

 1  <?xml version="1.0" encoding="UTF-8"?>
 2 <- log level, and prioritized:! OFF> FATAL> ERROR> WARN> INFO> DEBUG> TRACE> ALL ->
 3 <-! Configuration Status later, the information output log4j2 own internal set and may not be provided, when set to trace, one will see in detail the various internal outputs log4j2 ->
 4 <- monitorInterval:! Log4j can automatically detect and modify profiles reconfigure itself, provided the interval in seconds ->
 5  <configuration status="WARN" monitorInterval="30">
 6 <-! Define all appender ->
 7      <appenders>
 8 <-! The console output configuration ->
 9          <console name="Console" target="SYSTEM_OUT">
10 <! - log output format ->
11              <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
12          </console>
13 <! - file will print out all the information, the log will be automatically cleared each time you run the program, determined by the append attribute, this is also quite used for temporary test ->
14      <File name="log" fileName="log/test.log" append="false">
15         <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
16      </File>
17 <! - this will print out all the info and the following levels of information, each larger than size, this size is the size of the log will be automatically credited by year - File month created folder below and compression, as an archive - ->
18          <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log"
19                       filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
20 <! - only console output level and higher-level information (onMatch), other direct denial (onMismatch) ->        
21              <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
22              <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
23              <Policies>
24                  <TimeBasedTriggeringPolicy/>
25                  <SizeBasedTriggeringPolicy size="100 MB"/>
26              </Policies>
27          </RollingFile>
28          <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/warn.log"
29                       filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
30              <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
31              <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
32              <Policies>
33                  <TimeBasedTriggeringPolicy/>
34                  <SizeBasedTriggeringPolicy size="100 MB"/>
35              </Policies>
36 <-! DefaultRolloverStrategy attributes are not set, the default is the same up to seven file folder, there is provided 20 ->
37              <DefaultRolloverStrategy max="20"/>
38          </RollingFile>
39          <RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/error.log"
40                       filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
41              <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
42              <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
43              <Policies>
44                  <TimeBasedTriggeringPolicy/>
45                  <SizeBasedTriggeringPolicy size="100 MB"/>
46              </Policies>
47          </RollingFile>
48      </appenders>
49 <! - and then define the logger, only defines appender logger and introduced, appender take effect ->
50      <loggers>
51 <! - DEBUG filter out unwanted information and mybatis of spring ->
52          <logger name="org.springframework" level="INFO"></logger>
53          <logger name="org.mybatis" level="INFO"></logger>
54          <root level="all">
55              <appender-ref ref="Console"/>
56              <appender-ref ref="RollingFileInfo"/>
57              <appender-ref ref="RollingFileWarn"/>
58              <appender-ref ref="RollingFileError"/>
59          </root>
60      </loggers>
61  </configuration>

Copy the code

Guess you like

Origin blog.csdn.net/N_E_W_J_A_V_A/article/details/90208747