Logback learning is best to Logback official documentation, it is very fine, very complete.

configuration

Wrong configuration

    <! -- Log file output -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <! -- File name -->
        <File>${LOG_HOME}/${APP_NAME}/user.log</File>
        <! Determine the behavior of RollingFileAppender when scrolling occurs, involving file movement and renaming.
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/${APP_NAME}/user.log.%d{yyyy-mm-dd}.gz</FileNamePattern>
            <! If true, the log is appended to the end of the file. If false, the existing file is emptied. Default is true -->
            <! -- Maximum number of saved days -->
            <MaxHistory>10</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%thread{30}] %logger{50} : %msg%n</pattern>
        </encoder>
        <! -- Maximum size of log file -->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
Copy the code

This configuration will trigger archiving when the log file is larger than 10MB, but the FileNamePattern configuration time has not reached, and the log file has stopped exporting.

Correct configuration

    <! -- Log file output -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <! -- Log activation output file name -->
        <File>${LOG_HOME}/${APP_NAME}/auth.log</File>
        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>

        <! Determine the behavior of RollingFileAppender when scrolling occurs, involving file movement and renaming.
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <! When %d is used in multiple places, it is necessary to indicate that the main.gz or.zip compression is enabled automatically.
            <FileNamePattern>${LOG_HOME}/${APP_NAME}/%d{yyyy-MM-dd, aux}/auth.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
            <! If true, the log is appended to the end of the file. If false, the existing file is emptied. Default is true -->
            <! -- Maximum 2MB for each file, maximum 60 days data total size 3GB If a single file exceeds 2MB, trigger scrolling policy time trigger -->
            <MaxHistory>60</MaxHistory>
            <maxFileSize>2MB</maxFileSize>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
    </appender>
Copy the code

This configuration uses archiving policies based on time and size. When a single log file is larger than 2MB, archiving is triggered and indexes are added after the archive file name. The maximum size of archived files is 3GB and can be saved for a maximum of 60 days.

There is a small hole where an error will be detected if the value of maxFileSize is written one B short. As follows:

<maxFileSize>2M</maxFileSize>
Copy the code

appender

RollingFileAppender can archive log files and has two important subcomponents RollingPolicy and TriggeringPolicy. A TriggeringPolicy is responsible for when it triggers, and a RollingPolicy is responsible for what it does when it triggers.

TimeBasedRollingPolicy

  1. TimeBasedRollingPolicyTo achieve theRollingPolicyTriggeringPolicySo you don’t have to write it when you use itTriggeringPolicy
  2. TimeBasedRollingPolicyThe config has a mandatory attributefileNamePatternIs used to define the name of the archive file and infer the time of the archive.
  3. TimeBasedRollingPolicySupport automatic file compression, as long as the file to.gz.zipAt the end.

SizeAndTimeBasedRollingPolicy

SizeAndTimeBasedRollingPolicy can archive through time and at the same time limit the file size. % I is a must at this time. It is used when the size of the archive exceeds the limit before the current archiving by time. The serial number of each archive file is shown below:

FixedWindowRollingPolicy

From official documents

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>test.log</file>

    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <fileNamePattern>tests.%i.log.zip</fileNamePattern>
      <minIndex>1</minIndex>
      <maxIndex>3</maxIndex>
    </rollingPolicy>

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <maxFileSize>5MB</maxFileSize>
    </triggeringPolicy>
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>
        
  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>
Copy the code

This configuration triggers the FixedWindowRollingPolicy, which, as its name suggests, is a sliding window, when a single log file exceeds 5MB. Maintain a 1-3 log file, tests.1.log.zip, tests.2.log.zip, tests.3.log.zip. If test.log exceeds 5MB, tests.3.log.zip will be deleted, tests.2.log.zip will be renamed tests.3.log.zip, tests.1.log.zip will be renamed tests.2.log.zip, and tests.1.log.zip will be renamed tests.2.log.zip. Test.log is renamed tests.1.log.zip, and the new log continues to write test.log and so on

SizeBasedTriggeringPolicy

It detects the active log files and triggers the rollingPolicy for archiving once it exceeds a set threshold