Logback introduction and configuration file logback XML explanation

Posted by jeff8j on Sat, 05 Mar 2022 09:33:33 +0100

Introduction to logback

Logback is another open source log component designed by the founder of log4j. The official website is: http://logback.qos.ch.

  1. logback structure: it is mainly composed of three modules:
    • Logback core / / basic module. Other modules are based on it

    • Logback classic / / it is an improved version of log4j. At the same time, it fully implements the slf4j API and can be replaced with other logging systems, such as log4j

    • Logback access / / the access module is integrated with the Servlet container and provides the function of accessing logs through Http

  2. The reason for replacing log4j with it lies in the following advantages:

    It has achieved faster implementation, full testing, natural implementation of SLF4, very detailed official documents, automatic reload of configuration files, Lilith is the observer of log events, similar to chainsaw of log4j, cautious mode and very friendly recovery (multiple threads can write a log file at the same time), and the configuration file can handle different situations Filters, SiftingAppender, automatic compression of printed log files, stack tree with package version, automatic removal of old log files, etc.

logback. Detailed explanation of XML configuration file

  1. Configuration file example
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!-- Reload when the configuration file is modified. The default is true -->
    <configuration scan="true">
        
        <!--Define the storage address of the log file LogBack Relative paths are used in the configuration of-->
        <property name="CATALINA_BASE" value="**/logs"></property>
        
        <!-- console output  -->
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder charset="UTF-8">
                <!-- Output logging format -->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
     
        <!-- First file output,One file per day -->
        <appender name="FILE1" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- Output file path+file name -->
                <fileNamePattern>${CATALINA_BASE}/aa.%d{yyyyMMdd}.log</fileNamePattern>
                <!-- Keep a log for 30 days -->
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder charset="UTF-8">
                <!-- Output logging format -->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
     
        <!-- Second file output,One file per day -->
        <appender name="FILE2" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${CATALINA_BASE}/bb.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${CATALINA_BASE}/bb.%d{yyyyMMdd}.log</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder charset="UTF-8">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
        
        <appender name="CUSTOM" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${CATALINA_BASE}/custom.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- daily rollover -->
                <fileNamePattern>${CATALINA_BASE}/custom.%d{yyyy-MM-dd}.log</fileNamePattern>
                <!-- keep 30 days' worth of history -->
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder charset="UTF-8">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
        
        <!-- Set log output level -->
        <root level="ERROR">
            <appender-ref ref="CONSOLE" />
        </root>
        <logger name="file1" level="DEBUG">
            <appender-ref ref="FILE1" />
        </logger>
        <logger name="file1" level="INFO">
            <appender-ref ref="FILE2" />
        </logger>
        <!-- custom logger -->
        <logger name="custom" level="INFO">
            <appender-ref ref="CUSTOM" />
        </logger>
    </configuration>
    
  2. Profile description

    One element

    0 or more

    0 or more

  1. logback configuration file loading instructions

    When logback starts:

    1. Look for logback test in the classpath XML file
    2. If logback test is not found XML, then look for logback. XML in the classpath Groovy files
    3. If logback is not found Groovy, then look for logback in the classpath XML file
    4. If none of the above files can be found, logback will use the SPI mechanism of JDK to find meta-inf / services / ch.qos logback. classic. spi. The logback Configuration implementation class in the configurator must implement the Configuration interface and use its implementation for Configuration
    5. If none of the above operations are successful, logback will use its own basic configurator to configure and output the log to console
  2. Print level

    Trace < DEBUG < info < warn < error, default DEBUG

logback quick start

  1. Dependent jar packages:

    slf4j-api

    logback-core

    logback-classic

       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-api</artifactId>
           <version>1.7.5</version>
       </dependency>
       <dependency>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-core</artifactId>
           <version>1.0.11</version>
       </dependency>
       <dependency>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-classic</artifactId>
           <version>1.0.11</version>
       </dependency>
    

    [note] slf4j API is not a part of logback, but another project, which is usually used in combination

  2. Code examples (two types):
    // First, declare an object directly
    public class Slf4JLoggerTest1 {
        private static final Logger logger = LoggerFactory.getLogger(SimpleDemo.class);
        public static void main(String[] args) {
            logger.info("Hello,tese1, this is a line of log message logged by Logback");
        }
    }
    
    
    // The second is to add @slf4j annotations to the class configuration (* * implemented with lombok, the principle is the same as the first * *)
    @Slf4j
    public class Slf4JLoggerTest2 {
        public static void main(String[] args) {
            log.info("Hello,tese2, this is a line of log message logged by Logback");
        }
    }
    

reference material:

[1] logback configuration details https://segmentfault.com/a/1190000008315137

[2] use of logback and logback XML explanation https://www.cnblogs.com/warking/p/5710303.html

This article is from the blog Park and written by: ergwang For reprint, please indicate the original link: https://www.cnblogs.com/ergwang/p/14600913.html

Topics: Mybatis