Spring Boot integrates logback to achieve different log levels in different environments

  The article " IDEA creates a simple SpringBoot project (including setting up HTTP Proxy network proxy) " introduces the steps of Idea to create a Spring Boot project. Spring Boot integrates various log components, which is very convenient to use. In actual project configuration log output, some parameters need to be set to different values ​​according to different deployment environments. For example, the log output level of the production environment is INFO, and the log output level of the development environment is DEBUG. This article takes logback as an example to illustrate how to configure different parameters for different environments.

Method 1: Read the value in the spring configuration file

  The article " Spring Boot uses spring.profiles.active to distinguish different environment configurations " introduces Spring Boot's method of configuring different environment parameters. This article can set the log levels of different environments to the configuration files of different environments, and then use spring. profiles.active to specify the parameters of which environment to use.
  logback.xml is loaded earlier than application.yml and logback-spring.xml is loaded later than application.yml. If the logback configuration needs to use the properties in application.yml, it needs to be named logback-spring.xml.
  In this article, the logback-spring.xml file is used to read the property values ​​in the configuration file application-${profile}.properties. Logback needs to use the <springProperty> tag to use the properties in application.properties. When the property value does not exist in the configuration file, you can also set the default value. An example is shown below.

<!-- 读取 application.properties 中的 log.level 属性,如果没有配置,默认 INFO -->
<springProperty name="LOG_LEVEL" source="log.level" defaultValue="INFO"/>

<logger name="com.test.svc" level="${LOG_LEVEL}" additivity="false">
        <appender-ref ref="ASYNC"/>
        <appender-ref ref="STDOUT"/>
    </logger>
Method two, load the specified configuration module

  In the following configuration, the name in <springProfile name=“dev”> reads the value of the spring.profiles.active configuration item. If it is not set, the default is default.
  The following two mean that when spring.profiles.active is configured as dev, part of the configuration wrapped in it is loaded. When spring.profiles.active is not configured, or configured as default, the log settings wrapped in it are loaded. If there are other environments such as test, prod, release, etc., just follow this configuration. An expression can also follow the name.

<springProfile name="dev">
        <!-- 开发环境时激活 -->
        <logger name="com.test.svc" level="DEBUG" additivity="false">
            <appender-ref ref="ASYNC"/>
            <appender-ref ref="STDOUT"/>
        </logger>
</springProfile>
<springProfile name="default">
        <!-- 默认环境时激活 -->
        <logger name="com.test.svc" level="INFO" additivity="false">
            <appender-ref ref="ASYNC"/>
            <appender-ref ref="STDOUT"/>
        </logger>
</springProfile>
Appendix: A relatively complete logback-spring.xml configuration file is attached.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <springProperty name="log_dir" source="log_dir" defaultValue="D:/logs"/>
    <springProperty name="maxHistory" source="maxHistory" defaultValue="50"/>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/debug.%i.log.zip</fileNamePattern>
            <!--文件达到最大 128MB 时会被切割和压缩 -->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>128MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!-- 历史日志最大保留 50 天 -->
            <maxHistory>${maxHistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{req.remoteHost} %X{req.requestURI} %X{req.queryString} [%X{logId}] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <!-- 异步输出 -->
    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
        <!-- 不丢失日志,默认的,如果队列的 80% 已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默认的队列的深度,该值会影响性能,默认值为256 -->
        <queueSize>256</queueSize>
        <!-- 添加附加的appender,最多只能添加一个 -->
        <appender-ref ref="FILE"/>
    </appender>
    <!-- 日志打印到控制台 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <springProfile name="dev">
        <!-- 开发环境时激活 -->
        <logger name="com.test.svc" level="DEBUG" additivity="false">
            <appender-ref ref="ASYNC"/>
            <appender-ref ref="STDOUT"/>
        </logger>
    </springProfile>
    <springProfile name="test">
        <!-- 测试环境时激活 -->
        <logger name="com.test.svc" level="INFO" additivity="false">
            <appender-ref ref="ASYNC"/>
            <appender-ref ref="STDOUT"/>
        </logger>
    </springProfile>

    <logger name="root" level="ERROR">
        <appender-ref ref="ASYNC"/>
    </logger>
</configuration>
Reference documents:

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/110389773