Use logback to generate logs by day and classify them by level

Let’s see the effect first—->>>>

Classify according to: error, info, warn:

Each file is classified by date:

The corresponding Maven is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Add the following content to application.yml:

#Log configuration
logging:
  file:
    path: logs #content The logs folder in the root directory stores logs. If it does not exist, create it.
  pattern:
    console: "%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n"

Core file: logback-spring.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds">

    <springProperty scope="context" name="LOG_NAME" source="spring.application.name"/>
    <springProperty scope="context" name="LOG_PATH" source="logging.file.path"/>


    <!-- Log attribute configuration -->
    <property name="LOG_FILE" value="${LOG_PATH}/${LOG_NAME}.log"/>
    <property name="LOG_FILE_CLEAN_HISTORY_ON_START" value="false" />
    <property name="ROLLING_FILE_NAME_PATTERN" value="${LOG_FILE}.%d{yyyy-MM-dd}.%i" />
    <property name="LOG_FILE_MAX_SIZE" value="512MB" />
    <property name="LOG_FILE_MAX_HISTORY" value="30" />

    <contextName>${LOG_NAME}</contextName>
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--Formatted output (color matching): %d represents the date, %thread represents the thread name, %-5level: the level displays 5 characters from the left, width %msg: log message, %n is the newline character -->
            <pattern>%boldMagenta(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %cyan(%logger{50}) - %msg %n </pattern>
        </encoder>
    </appender>


    <!--Separate logs according to log levels and output them to different files -->
    <appender name="debugLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
        <!--Rolling Strategy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--Save logs by time. Modify the format to save by hour, day, or month-->
            <fileNamePattern>${LOG_PATH}/${LOG_NAME}-debug.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!--Save duration-->
            <MaxHistory>${LOG_FILE_MAX_HISTORY}</MaxHistory>
            <!--File size-->
            <totalSizeCap>${LOG_FILE_MAX_SIZE}</totalSizeCap>
        </rollingPolicy>
    </appender>


    <!--Separate logs according to log levels and output them to different files -->
     <appender name="infoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
        <!--Rolling Strategy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--Save logs by time. Modify the format to save by hour, day, or month-->
            <fileNamePattern>${LOG_PATH}/${LOG_NAME}-info.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!--Save duration-->
            <MaxHistory>${LOG_FILE_MAX_HISTORY}</MaxHistory>
            <!--File size-->
            <totalSizeCap>${LOG_FILE_MAX_SIZE}</totalSizeCap>
        </rollingPolicy>
    </appender>


    <appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
        <!--Rolling Strategy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--Path-->
            <fileNamePattern>${LOG_PATH}/${LOG_NAME}-error.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!--Save duration-->
            <MaxHistory>${LOG_FILE_MAX_HISTORY}</MaxHistory>
            <!--File size-->
            <totalSizeCap>${LOG_FILE_MAX_SIZE}</totalSizeCap>
        </rollingPolicy>
    </appender>

    <!--If you don’t need to distinguish the spring environment, use the following code directly-->
    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="infoLog"/>
        <appender-ref ref="errorLog"/>
    </root>

<!--If you need to distinguish springfile, you need to use the following code-->
<!-- <springProfile name="local">
        <root level="debug">
            <appender-ref ref="consoleLog"/>
        </root>
    </springProfile>


    <springProfile name="dev">
        <root level="info">
            <appender-ref ref="consoleLog"/>
            <appender-ref ref="infoLog"/>
            <appender-ref ref="errorLog"/>
            <appender-ref ref="JSON_FILE" />
        </root>
    </springProfile>

    <springProfile name="test">
        <root level="info">
            <appender-ref ref="infoLog"/>
            <appender-ref ref="errorLog"/>
            <appender-ref ref="JSON_FILE" />
        </root>
    </springProfile>

    <springProfile name="prod">
        <root level="info">
            <appender-ref ref="consoleLog"/>
            <appender-ref ref="infoLog"/>
            <appender-ref ref="errorLog"/>
            <appender-ref ref="JSON_FILE" />
        </root>
    </springProfile>-->


</configuration>