1. Configuration file format

1. The root tag

< Configuration > is the root tag of the Logback configuration file, and all the configuration content must be defined in the < Configuration > tag. The < Configuration > tag contains the following three commonly used properties:

  • Scan: When the value is true, the system reloads the configuration file if the content of the configuration file changes. The default value is true.
  • ScanPeriod: sets the interval for monitoring configuration file changes. The default value is60 seconds, 60s. If the unit is default, the default unit is millisecond. In addition, scanPeriod is valid only when the scan attribute is true.
  • Debug: Indicates whether to print internal logback run logs. The default value is false. Generally, you do not need to care about the internal running status of logback.

Example:


      
<configuration scan="true" scanPeriod="60 seconds" debug="false">
</configuration>
Copy the code

2. Variable definition

The tag defines variables that can be referenced later as ${variable name}. The tag has two property values:

  • Name: Defines the variable name
  • Value: Defines the value of a variable

Example:


      
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <! -- Define log directory -->
    <property name="DEBUG_LOG_PATH" value="logs/debug"/>
    <! Define log retention days -->
    <property name="MAX_HISTORY" value="Seven"/>
    <! Define total log file size -->
    <property name="TOTAL_SIZE_CAP" value="10GB"/>
    <! Define a single log file size -->
    <property name="MAX_FILE_SIZE" value="50MB"/>
</configuration>
Copy the code

3. Output logs

The

tag is used to configure log output and is the most important tag. The

tag has two properties:

  • Name: Specifies the name of the appender
  • Class: specifies the log output policy, wherech.qos.logback.core.ConsoleAppenderTo specify log output to the console,ch.qos.logback.core.rolling.RollingFileAppenderUsed to specify log output to a scroll file
3.1 Log Format

The

tag is used to format the log.

has two children and

. specifies the log format, and

specifies the encoding.

Example:


      
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <! -- Output to console -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
</configuration>
Copy the code

Log output format:

  • %d{YYYY-MM-DD HH: MM :ss} : indicates the date. The format of the date in braces isyyyy-MM-dd HH:mm:ss
  • %thread: indicates the name of the thread
  • %-5level: indicates the log level. -5 indicates the width of five characters displayed from the left
  • %logger{36} : indicates the logger name. It is 36 characters wide
  • % MSG: indicates the log content
  • %n: newline
3.2 Log File Name

The

tag is used to specify a log file, which can be a relative or absolute path. If the parent directory does not exist, it will be created automatically. The

tag can only be used if the appender’s log output policy is log files or scroll files.

Example:


      
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <! -- Define log directory -->
    <property name="DEBUG_LOG_PATH" value="logs/debug"/>
    
    <! -- Output to scroll file -->
    <appender name="DEBUG_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEBUG_LOG_PATH}/output.log</file>
        <encoder>
            <! -- Log output format -->
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
</configuration>
Copy the code
3.3 Rolling Policy

The

tag is used to specify the rollingPolicy, which is essentially the archiving of log files. The

tag has only one class attribute, with the following common attribute values:

  • ch.qos.logback.core.rolling.TimeBaseRollingPolicy: time-based rolling policy
  • ch.qos.logback.core.rolling.SizeAndTimeBaseRollingPolicy: scroll policy based on file size and time

The sublabels of

are as follows:

  • <fileNamePattern>: Specifies the archived log file name
  • <maxHistory>: Specifies the retention days of logs
  • <totalSizeCap>: Specifies the total size of the archive file, beyond which old log files will be deleted
  • <maxFileSize>: Specifies the size limit for log files, used in the file size and time scrolling policy

Example:


      
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <! -- Define log directory -->
    <property name="DEBUG_LOG_PATH" value="logs/debug"/>
    <! Define log retention days -->
    <property name="MAX_HISTORY" value="Seven"/>
    <! Define total log file size -->
    <property name="TOTAL_SIZE_CAP" value="10GB"/>
    <! Define a single log file size -->
    <property name="MAX_FILE_SIZE" value="50MB"/>
    
    <! -- Output to console -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <! -- Log output format -->
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    
    <! -- Output to scroll file -->
    <appender name="DEBUG_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEBUG_LOG_PATH}/output.log</file>
        <! -- Scroll strategy based on file size and time -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${DEBUG_LOG_PATH}/output-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <! -- Log file retention days -->
            <maxHistory>${MAX_HISTORY}</maxHistory>
            <! -- Total size of log archive files -->
            <totalSizeCap>${TOTAL_SIZE_CAP}</totalSizeCap>
            <! -- Single log file size -->
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
        </rollingPolicy>
        <! -- Log output format -->
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
</configuration>
Copy the code
3.4 Log Filtering

Log level: TRACE < DEBUG < INFO < WARN < ERROR

If only specific logs need to be logged, you can skip the logs using the

tag. The

tag has only one attribute, class, which specifies the skipper. Common filters are LevelFilter, which filters logs by log level (other filters do not go into too much detail).

Example:


      
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <! -- Define log directory -->
    <property name="DEBUG_LOG_PATH" value="logs/debug"/>
    <! Define log retention days -->
    <property name="MAX_HISTORY" value="Seven"/>
    <! Define total log file size -->
    <property name="TOTAL_SIZE_CAP" value="10GB"/>
    <! Define a single log file size -->
    <property name="MAX_FILE_SIZE" value="50MB"/>
    
    <! -- Output to console -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <! -- Log output format -->
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    
    <! -- Output DEBUG log -->
    <appender name="DEBUG_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEBUG_LOG_PATH}/output.log</file>
        <! -- Scroll strategy based on file size and time -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${DEBUG_LOG_PATH}/output-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <! -- Log file retention days -->
            <maxHistory>${MAX_HISTORY}</maxHistory>
            <! -- Total size of log archive files -->
            <totalSizeCap>${TOTAL_SIZE_CAP}</totalSizeCap>
            <! -- Single log file size -->
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
        </rollingPolicy>
        <! -- Log output format -->
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <! -- Log filtering -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <! -- Specify log level -->
            <level>DEBUG</level>
            <! -->
            <onMatch>ACCEPT</onMatch>
            <! -- If no match, reject all -->
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
</configuration>
Copy the code

4. The root logger

The

tag is a mandatory tag in the configuration file. It is also a special

tag that specifies the basic log output level. The

tag has only one attribute, level, which specifies the log level.


The

tag is used to define the log output level for a particular package. Appenders need to be bound to Logger or the defined appender will not take effect. (Bound via the ref attribute in
).


has three attributes:

  • Name: Mandatory property to specify the package name
  • Level: Indicates the optional log level. The default value isDEBUG
  • Addtivity: Whether to pass log output to a parent logger(typically root), default true.

Tips: If the addtivity property is true, logs that are generated in the appender bound to the current logger are generated in the appender bound to the root logger

Example:

In the following example, logs sent to INFO_LOG_FILE are also sent to CONSOLE and DEBUG_LOG_FILE appenders

<! -- root -->
<root level="DEBUG">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="DEBUG_LOG_FILE"/>
</root>
<! -- logger -->
<logger name="com.example" level="INFO" addtivity="true">
    <appender-ref ref="INFO_LOG_FILE"/>
</logger>
Copy the code

5. Configure multiple environments

On the SpringBoot official website, it is recommended that the logback configuration file be named *-spring. XML. In this way, some SpringBoot features can be used in the configuration file, for example, different levels of logs are generated in different environments.

The

tag is used to specify the SpringBoot profile, as shown below:

<! -- Set profile to default and prod -->
<springProfile name="default,prod">
    <root level="ERROR">
        <appender-ref ref="ERROR_LOG_FILE"/>
    </root>
</springProfile>
<! Dev and test -->
<springProfile name="dev,test">
    <root level="DEBUG">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="DEBUG_LOG_FILE"/>
        <appender-ref ref="ERROR_LOG_FILE"/>
    </root>
</springProfile>
Copy the code

2. Configuration file template

Here is the logback-spring. XML configuration file template:


      
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <! -- Define log directory -->
    <property name="DEBUG_LOG_PATH" value="logs/debug"/>
    <property name="INFO_LOG_PATH" value="logs/info"/>
    <property name="WARN_LOG_PATH" value="logs/warn"/>
    <property name="ERROR_LOG_PATH" value="logs/error"/>
    <! Define log retention days -->
    <property name="MAX_HISTORY" value="Seven"/>
    <! Define total log file size -->
    <property name="TOTAL_SIZE_CAP" value="10GB"/>
    <! Define a single log file size -->
    <property name="MAX_FILE_SIZE" value="50MB"/>
   
    <! -- Output to console -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <! -- Log output format -->
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    
    <! -- Output only DEBUG logs -->
    <appender name="DEBUG_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEBUG_LOG_PATH}/output.log</file>
        <! -- Scroll strategy based on file size and time -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${DEBUG_LOG_PATH}/output-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <! -- Log file retention days -->
            <maxHistory>${MAX_HISTORY}</maxHistory>
            <! -- Total size of log archive files -->
            <totalSizeCap>${TOTAL_SIZE_CAP}</totalSizeCap>
            <! -- Single log file size -->
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
        </rollingPolicy>
        <! -- Log output format -->
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <! -- Log filtering -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <! -- Specify log level -->
            <level>DEBUG</level>
            <! -->
            <onMatch>ACCEPT</onMatch>
            <! -- If no match, reject all -->
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    
    <! -- Only output INFO log -->
    <appender name="INFO_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${INFO_LOG_PATH}/output.log</file>
        <! -- Scroll strategy based on file size and time -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${INFO_LOG_PATH}/output-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <! -- Log file retention days -->
            <maxHistory>${MAX_HISTORY}</maxHistory>
            <! -- Total size of log archive files -->
            <totalSizeCap>${TOTAL_SIZE_CAP}</totalSizeCap>
            <! -- Single log file size -->
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
        </rollingPolicy>
        <! -- Log output format -->
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <! -- Log filtering -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <! -- Specify log level -->
            <level>INFO</level>
            <! -->
            <onMatch>ACCEPT</onMatch>
            <! -- If no match, reject all -->
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    
    <! WARN logs only -->
    <appender name="WARN_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${WARN_LOG_PATH}/output.log</file>
        <! -- Scroll strategy based on file size and time -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${WARN_LOG_PATH}/output-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <! -- Log file retention days -->
            <maxHistory>${MAX_HISTORY}</maxHistory>
            <! -- Total size of log archive files -->
            <totalSizeCap>${TOTAL_SIZE_CAP}</totalSizeCap>
            <! -- Single log file size -->
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
        </rollingPolicy>
        <! -- Log output format -->
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <! -- Log filtering -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <! -- Specify log level -->
            <level>WARN</level>
            <! -->
            <onMatch>ACCEPT</onMatch>
            <! -- If no match, reject all -->
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    
    <! -- ERROR only -->
    <appender name="ERROR_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${ERROR_LOG_PATH}/output.log</file>
        <! -- Scroll strategy based on file size and time -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${ERROR_LOG_PATH}/output-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <! -- Log file retention days -->
            <maxHistory>${MAX_HISTORY}</maxHistory>
            <! -- Total size of log archive files -->
            <totalSizeCap>${TOTAL_SIZE_CAP}</totalSizeCap>
            <! -- Single log file size -->
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
        </rollingPolicy>
        <! -- Log output format -->
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}: %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <! -- Log filtering -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <! -- Specify log level -->
            <level>ERROR</level>
            <! -->
            <onMatch>ACCEPT</onMatch>
            <! -- If no match, reject all -->
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    
    <! -- Multi-environment configuration -->
    <springProfile name="default,prod">
        <! -- root -->
        <root level="INFO">
            <appender-ref ref="INFO_LOG_FILE"/>
            <appender-ref ref="ERROR_LOG_FILE"/>
        </root>
	</springProfile>
    
    <! -- Multi-environment configuration -->
    <springProfile name="dev,test">
        <! -- root -->
        <root level="DEBUG">
            <appender-ref ref="CONSOLE"/>
            <appender-ref ref="DEBUG_LOG_FILE"/>
            <appender-ref ref="INFO_LOG_FILE"/>
            <appender-ref ref="WARN_LOG_FILE"/>
            <appender-ref ref="ERROR_LOG_FILE"/>
        </root>
	</springProfile>
</configuration>
Copy the code

Reference documentation

  • Logback official document
  • Logback Chinese manual