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:
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=true
or in the .yaml configuration file, debug: true
more 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 LEVEL
can 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.groovy
placing 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)