While the previous article probably covered Logback, this one focuses on XML configuration files. Here is a typical XML file that covers most common configuration items, each of which is annotated to make it intuitive. The file name is logback-spring. XML and is placed in the Resources directory of spring Boot.


      
<! -- Scan defaults to true and automatically reloads XML when it changes; Debug Default false, true to print logs -->
<configuration scan="false" scanPeriod="60 seconds" debug="false">

<! -- variable name and value, can be referenced by ${variable name} -->
    <property name="APP_NAME" value="blog" />
    <property name="LOG_HOME" value="./logs" />

<! This appender outputs logs to the console.
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<! -- Format the log -->
        <encoder>
<! -- %d table date, %thread table thread name, %-5level table level (displayed from left 5 characters wide) % LOGGER Class name of the output log, {25} maximum of 25 characters %line number, % MSG log content, %n newline character -- -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{25}:%line %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

<! The destination of this appender is the scroll record file -->
    <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<! -- TimeBasedRollingPolicy -- TimeBasedRollingPolicy
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<! Logs /blog-xxx.log --> logs/blog-xxx.log -->
            <fileNamePattern>${LOG_HOME}/${APP_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern>
<! -- The maximum number of log files to be retained, then delete the old files, the following is retained for 7 months -->
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{25}:%line %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

<! This appender is an asynchronous output log file -->
    <appender name="asyncFile" class="ch.qos.logback.classic.AsyncAppender">
<! When the remaining queue capacity is 20%, low-level events such as INFO, DEBUG, and TRACE are not discarded. The default - 1 - >
        <discardingThreshold>0</discardingThreshold>
<! -- Queue depth, default: 256-->
        <queueSize>2048</queueSize>
<! Extract caller information data, such as line numbers. Has little impact on performance, default false-->
        <includeCallerData>true</includeCallerData>
<! An appender named rollingFile that outputs it asynchronously -->
        <appender-ref ref="rollingFile" />
    </appender>

<! -- Log print level for a package or a class -->
    <logger name="org.springframework" level="WARN"/>
    <logger name="org.hibernate.SQL" level="WARN"/>

<! -- Using spring extension profile support, this configuration file should be named logback-spring.xml -->
<! -- Log configuration in development environment -->
    <springProfile name="dev">
<! Root logger, specify log level, which appender to use, and multiple appenders.
        <root level="info">
            <appender-ref ref="stdout" />
            <! -- <appender-ref ref="rollingFile" /> -->
        </root>
    </springProfile>
<! -- Log configuration in H2 -->
    <springProfile name="h2">
        <root level="info">
            <appender-ref ref="stdout" />
        </root>
    </springProfile>
<! Docker -->
    <springProfile name="docker">
        <root level="info">
            <appender-ref ref="asyncFile" />
        </root>
    </springProfile>

</configuration>
Copy the code