Section 11: spring boot integration log4j2 log

SpringBoot uses logback by default, but there is a higher performance logging implementation framework log4j2

Why log4j2

Compared with other log systems, log4j2 has less data loss; The performance of disruptor technology is more than 10 times higher than that of logback in multi-threaded environment; Using jdk1 5 concurrency, which reduces the occurrence of deadlock;

The following is a performance comparison picture from the network

The performance of Logback is the worst in synchronous log mode, and the performance of log4j2 is the best in both synchronous log mode and asynchronous log mode

The main reason for the high performance of log4j2 is that it uses an LMAX lockless inter thread communication library

pom. Introducing log4j2 into XML

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions><!-- Remove springboot Default configuration -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency><!-- introduce log4j2 rely on -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>

Configuration file for log4j2

The default name is log4j2 spring xml. For other names, you need to specify logging. In the configuration file config=xxx. xml

Profile template

<?xml version="1.0" encoding="UTF-8"?>
<!--Configuration hinder status,This is used to set log4j2 The internal information output can not be set when it is set to trace You'll see log4j2 Various internal detailed outputs-->
<!--monitorInterval: Log4j It can automatically detect and modify the configuration file and reconfigure itself, and set the interval seconds-->
<configuration monitorInterval="5">
  <!--Log level and prioritization: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->

  <!--Variable configuration-->
  <Properties>
    <!-- Format output:%date Indicates the date,%thread Represents the thread name,%-5level: The level is displayed 5 characters wide from the left %msg: Log messages,%n Is a newline character-->
    <!-- %logger{36} express Logger The maximum length of the first name is 36 characters -->
    <property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
    <!-- Define the path of log storage -->
    <property name="FILE_PATH" value="Change to your log path" />
    <property name="FILE_NAME" value="Change to your project name" />
  </Properties>

  <appenders>

    <console name="Console" target="SYSTEM_OUT">
      <!--Format of output log-->
      <PatternLayout pattern="${LOG_PATTERN}"/>
      <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)-->
      <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
    </console>

    <!--The file will print out all the information, this log The program will be emptied automatically every time it runs, and the append Attribute, suitable for temporary testing-->
    <File name="Filelog" fileName="${FILE_PATH}/test.log" append="false">
      <PatternLayout pattern="${LOG_PATTERN}"/>
    </File>

    <!-- This will print out all the info And below, each time the size exceeds size,Then this size Logs of size are automatically saved by year-Under the folder created in the month and compressed as an archive-->
    <RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/info.log" filePattern="${FILE_PATH}/${FILE_NAME}-INFO-%d{yyyy-MM-dd}_%i.log.gz">
      <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)-->
      <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
      <PatternLayout pattern="${LOG_PATTERN}"/>
      <Policies>
        <!--interval Property is used to specify how often to scroll. The default is 1 hour-->
        <TimeBasedTriggeringPolicy interval="1"/>
        <SizeBasedTriggeringPolicy size="10MB"/>
      </Policies>
      <!-- DefaultRolloverStrategy If the property is not set, the default is to overwrite up to 7 files in the same folder-->
      <DefaultRolloverStrategy max="15"/>
    </RollingFile>

    <!-- This will print out all the warn And below, each time the size exceeds size,Then this size Logs of size are automatically saved by year-Under the folder created in the month and compressed as an archive-->
    <RollingFile name="RollingFileWarn" fileName="${FILE_PATH}/warn.log" filePattern="${FILE_PATH}/${FILE_NAME}-WARN-%d{yyyy-MM-dd}_%i.log.gz">
      <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)-->
      <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
      <PatternLayout pattern="${LOG_PATTERN}"/>
      <Policies>
        <!--interval Property is used to specify how often to scroll. The default is 1 hour-->
        <TimeBasedTriggeringPolicy interval="1"/>
        <SizeBasedTriggeringPolicy size="10MB"/>
      </Policies>
      <!-- DefaultRolloverStrategy If the property is not set, the default is to overwrite up to 7 files in the same folder-->
      <DefaultRolloverStrategy max="15"/>
    </RollingFile>

    <!-- This will print out all the error And below, each time the size exceeds size,Then this size Logs of size are automatically saved by year-Under the folder created in the month and compressed as an archive-->
    <RollingFile name="RollingFileError" fileName="${FILE_PATH}/error.log" filePattern="${FILE_PATH}/${FILE_NAME}-ERROR-%d{yyyy-MM-dd}_%i.log.gz">
      <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)-->
      <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
      <PatternLayout pattern="${LOG_PATTERN}"/>
      <Policies>
        <!--interval Property is used to specify how often to scroll. The default is 1 hour-->
        <TimeBasedTriggeringPolicy interval="1"/>
        <SizeBasedTriggeringPolicy size="10MB"/>
      </Policies>
      <!-- DefaultRolloverStrategy If the property is not set, the default is to overwrite up to 7 files in the same folder-->
      <DefaultRolloverStrategy max="15"/>
    </RollingFile>

  </appenders>

  <!--Logger Node is used to specify the form of log separately, for example, for the log under the specified package class Specify different log levels, etc.-->
  <!--Then define loggers,Only defined logger And introduced appender,appender Will take effect-->
  <loggers>

    <!--Filter out spring and mybatis Some useless DEBUG information-->
    <logger name="org.mybatis" level="info" additivity="false">
      <AppenderRef ref="Console"/>
    </logger>
    <!--Monitoring system information-->
    <!--If additivity Set as false,Zezi Logger Only in their own appender Output in, not in the parent Logger of appender Output in.-->
    <Logger name="org.springframework" level="info" additivity="false">
      <AppenderRef ref="Console"/>
    </Logger>
    <root level="info"> <!--Define log level,Greater than or equal to info All level logs will be output-->
      <appender-ref ref="Console"/>
      <appender-ref ref="Filelog"/>
      <appender-ref ref="RollingFileInfo"/>
      <appender-ref ref="RollingFileWarn"/>
      <appender-ref ref="RollingFileError"/>
    </root>
  </loggers>
</configuration>

configuration

  • status is used to specify the print log level of log4j itself
  • monitorInterval through the monitorInterval configuration parameter, you can dynamically monitor whether the configuration file has been modified. If so, you can dynamically modify the priority of the logger according to the latest configuration file

Properties

Variable definition

  • property defines a variable in the following format
<!-- Format output:%date Indicates the date,%thread Represents the thread name,%-5level: The level is displayed 5 characters wide from the left %msg: Log messages,%n Is a newline character-->
<!-- %logger{36} express Logger The maximum length of the first name is 36 characters -->
<property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />

appenders

  • Console defines the Appender that is output to the console
//Child element

name:appoint Appender Name of.
target:SYSTEM_OUT or SYSTEM_ERR,Generally, only default settings are set:SYSTEM_OUT.
PatternLayout:Output format, not set, default to:%m%n.
ThresholdFilter: `<!--Console output only level(attribute)And above( onMatch),Other direct rejection( onMismatch)-->`
  • File is used to define the Appender to export to the specified location file
  • RollingFile defines the deletion and creation policies of logs that exceed the specified conditions
name:appoint Appender Name of.
fileName:Specify the destination file of the output log and the file name with full path.
PatternLayout:Output format, not set, default to:%m%n.
filePattern:Specifies the name format of the new log file.
Policies:Specify the strategy of rolling logs, that is, when to create new log files and output logs,Child element`TimeBasedTriggeringPolicy interval="1" `interval Property is used to specify how often to scroll. The default is 1 hour. `SizeBasedTriggeringPolicy size="10MB"`,size Property is used to define the size of each log file.

loggers

  • logger is used to specify the log format separately
  • Root is used to specify the root log of the project. If Logger is not specified separately, the root log output will be used by default

    level Root Properties of. There are 8 log output levels in total,Priority from large to small OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL
     The higher the log level is,The fewer logs you output
    `appender-ref` Root The child node of the log, which is used to specify to which the log is output Appender

Source code address of this summary:

introduce

  • Follow the [entry station] and reply to [1001] to get the quick reference manual of common linux commands
  • Follow the [entry station] reply [1003] to get the solution of LeetCode [java language implementation]
  • Pay attention to [entry station] and reply to [1004] to get the summary of Java basic core
  • Follow [entry site] and reply to [1009] to obtain Alibaba Java development manual

Keywords: Spring Boot log4j2

Added by mwaw on Mon, 14 Feb 2022 05:53:30 +0200