SpringBoot: configuration of logback

created at 12-16-2021 views: 12

Preface

To realize the function: according to different configuration files, the logback log save path is different
For example: the development environment Win10, the log saving path is D:\log, and the running environment Linux system, the log saving path is /log

Configure logback

Add dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>

Create a new logback-spring.xml configuration file under the resources directory and add the following content:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- The name value is the variable name, the source value is the variable value, that is, the log save path, read the application.yml configuration, no need for ${} -->
    <springProperty scope="context" name="LOG_HOME" source="logging.file.path"/>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS}][%p][%c{40}][%t] %m%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
    </appender>

    <appender name="mes" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!-- Call log path variable via ${LOG_HOME} -->
        <File>${LOG_HOME}lmes.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}lmes.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
            <maxHistory>10</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS}][%p][%c{40}][%t] %m%n</pattern>
        </encoder>
    </appender>

    <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}lmes.error.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}lmes.error.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
            <maxHistory>10</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS}][%p][%c{40}][%t] %m%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <logger name="com.mes" additivity="false" level="INFO">
        <appender-ref ref="mes"/>
        <appender-ref ref="console"/>
    </logger>

    <logger name="com.mes.dao" level="DEBUG"/>

    <root level="DEBUG">
        <appender-ref ref="console"/>
        <appender-ref ref="error"/>
    </root>

</configuration>

In the application-dev.yml configuration file, add logback log configuration

# Windows environment
logging:
   config: classpath:logback-spring.xml
   file:
     path: C:\log

# Linux environment
logging:
   config: classpath:logback-spring.xml
   file:
     path: /log
created at:12-16-2021
edited at: 12-16-2021: