Detailed Explanation of LogBack Configuration of SpringBoot

  • 2021-07-01 07:31:12
  • OfStack

LogBack is integrated in Spring Boot by default and is a logging framework based on Slf4j. By default, Spring and Boot are output to the console at the INFO level.

Its log level is:

ALL < TRACE < DEBUG < INFO < WARN < ERROR < OFF

Configure

LogBack can be configured directly in application. properties or application. yml, but only some simple configurations are supported, and complex file output still needs to be configured in xml configuration file. The configuration file can be named logback. xml, and LogBack automatically searches for the configuration file under the root directory of classpath, but Spring Boot is recommended to be named logback-spring. xml, which automatically introduces some extensions of Spring Boot 1.

If you need to introduce a configuration file with a custom name, you need to specify it in the configuration file of Spring Boot, such as:


logging:
 config: classpath:logback-spring.xml

At the same time, Spring Boot provides a default base. xml configuration, which can be introduced as follows:


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml"/>
</configuration>

base. xml provides 1 basic default configuration and keyword color matching in console output. The specific file content can be seen here, and 1 commonly used configuration writing can be seen.

Detailed configuration

Variable

You can use the < property > To define variables:


<property name="log.path" value="/var/logs/application" />

Environment variables of Spring can also be introduced:


<property resource="application.yml" />
<property resource="application.properties" />

All variables can be called through ${}.

Output to console


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
  <encoder>
   <pattern>%.-1level|%-40.40logger{0}|%msg%n</pattern>
  </encoder>
 </appender>
 
 <logger name="com.mycompany.myapp" level="debug" />
 <logger name="org.springframework" level="info" />
 <logger name="org.springframework.beans" level="debug" />
 
 <root level="warn">
  <appender-ref ref="console" />
 </root>
</configuration>

Output to file


<property name="LOG_FILE" value="LogFile" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>${LOG_FILE}.log</file>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <!--  Daily archive log file  -->
    <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern>
    <!--  Reservation  30  Archived log files for days  -->
    <maxHistory>30</maxHistory>
    <!--  Log file upper limit  3G The old archive log file will be deleted when it is exceeded  -->
    <totalSizeCap>3GB</totalSizeCap>
  </rollingPolicy>
  <encoder>
    <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
  </encoder>
</appender> 

Multi-environment configuration

LogBack also supports multiple environment configurations, such as dev, test, prod


<springProfile name="dev">
  <logger name="com.mycompany.myapp" level="debug"/>
</springProfile>

java-jar xxx. jar-spring. profiles. active = dev to make the configuration work.

If you want to use the profile support of the Spring extension, the configuration file name must be named LogBack_Spring. xml, and the above configuration will only take effect when spring. profiles. active = dev is specified in application. properties.


Related articles: