Spring Boot log configuration

The logging frameworks supported by Spring Boot are: Java Util Logging, Log4J and Logback. We can see that Logback is used by default and uses INFO level output:
write picture description here

Output log level control

If we start the project by starting the jar package, the command to start the debug mode by pressing debug is:

 java -jar .\spring-boot-0.0.1-SNAPSHOT.jar --debug

If added in the .properties configuration file debug=trueor in the .yaml configuration file, debug: truemore content will be output in the background, but the DEBUG output level will not be enabled.

1. The default configuration file configuration (not recommended: not flexible enough)


Configuration format in .properties: logging.level.* = LEVEL,
1. It LEVELcan be TRACE,DEBUG,INFO,WARN,ERROR,FATAL, OFF
2. It *is the package name or the Logger name
For example :

logging.level.com.example.mx=WARN
logging:
  level:
    com.example.mx:
      DEBUG

2. Referencing external configuration files


Logback configuration method
  Spring Boot will load the file by default , and you can   use the custom configuration method by logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovyplacing the new one under src/main/resources :

logging.config=classpath:logging-config.xml

    Note: Spring Boot officially recommends to use the file name with -spring as your log configuration (for example, use logback-spring.xml instead of logback.xml), do not use logback for the name, otherwise Spring Boot will not be fully instantiated .

If you don't want to use the default name as the log configuration file, .properties can configure the log file like:

logging.config=classpath:logging-config.xml

3. Multi-environment output log


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

    <!-- 文件输出格式 -->
    <property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" />
    <!-- test文件路径 -->
    <property name="TEST_FILE_PATH" value="/logs" />
    <!-- pro文件路径 -->
    <property name="PROD_FILE_PATH" value="/logs" />

    <!-- 开发环境 -->
    <springProfile name="dev">
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${PATTERN}</pattern>
            </encoder>
        </appender>

        <logger name="com.example.mx" level="debug"/>

        <root level="DEBUG">
            <appender-ref ref="CONSOLE" />
        </root>
    </springProfile>

    <!-- 测试环境 -->
    <springProfile name="test">
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${PATTERN}</pattern>
            </encoder>
        </appender>
        <!-- 每天产生一个文件 -->
        <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${TEST_FILE_PATH}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
                <!-- 文件最大保存历史数量 -->
                <MaxHistory>100</MaxHistory>
            </rollingPolicy>

            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>${PATTERN}</pattern>
            </layout>
        </appender>

        <!-- <logger name="com.example.mx" level="debug"/> -->

        <root level="debug">
            <appender-ref ref="TEST-FILE" />
            <appender-ref ref="CONSOLE" />
        </root>
    </springProfile>

    <!-- 生产环境 -->
    <springProfile name="prod">
        <appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${PRO_FILE_PATH}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${PROD_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
                <MaxHistory>100</MaxHistory>
            </rollingPolicy>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>${PATTERN}</pattern>
            </layout>
        </appender>

        <root level="warn">
            <appender-ref ref="PROD_FILE" />
        </root>
    </springProfile>
</configuration>

Configure in .properties Configure spring.profiles.active=test
in .yaml

spring:
  profiles:
    active: dev

You can choose the configuration in springProfile


For more detailed configuration, please refer to: Spring Boot log configuration (super detailed)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325975048&siteId=291194637