Spring-boot project integrates logback

Using the spring-boot project to add log output, there are two large programs log4j/log4j2 and logback for java log output. log4j2 is an upgraded version of log4j.
The conventional practice is to introduce slf4j as the log entry, and choose one of log4j or logback for implementation. In the spring project, only spring-boot-starter-web uses log4j, and all other used starters are logback.

依赖 dependency-spring-boot-starter-logging

spring-boot-starter-logging also has two very confusing variants of starters
spring-boot-starter-log4j and spring-boot-starter-log4j2

The former stopped after the version was updated to 1.3, and the latter continued to be updated, so you need to pay attention.

dependencies

spring-boot-starter-logging dependencies are

groupId artifactId version
ch.qos.logback logback-classic 1.2.3
org.apache.logging.log4j log4j-to-slf4j 2.10.0
org.slf4j jul-to-slf4j 1.7.25

The above list shows that the final printing of log is implemented using logback. Spring-boot-starter-logging is preferred in the project

logback configuration

First look at a typical configuration file

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <property name="name" value="spring-boot-logging"/>
    <contextName>${name}</contextName>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/infisa.${name}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/${name}.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- project default level -->
    <logger name="cn.lee.jason" level="info"/>

    <!-- 定义根日志级别 -->
    <root level="info">
        <appender-ref ref="rollingFile"/>
        <appender-ref ref="console"/>
    </root>
   
</configuration>

configuration

Configuration is the root node of logback.xml. All configuration items must be located under the configuration element. The configurable attributes are:

property name Defaults illustrate
scan true When this property is set to true, the configuration file will be reloaded if changed
scanPeriod 1min Sets the time interval for monitoring whether the configuration file is modified. If no time unit is given, the default unit is milliseconds. This property takes effect when scan is true.
debug true When this property is set to true, the internal log information of logback will be printed out, and the running status of logback will be viewed in real time.

contextName

Each logger is associated with a logger context, the default context name is "default". but can use

property

labels used to define variable values,

appender

The component responsible for writing logs, it has two required attributes name and class. name specifies the appender name and class specifies the fully qualified name of the appender.

Console Appender

The log is output to the console and has the following subnodes:

FileAppender

Add the log to a file with the following subnodes:
      

RollingFileAppender

Scroll the record file, first record the log to the specified file, and when a certain condition is met, record the log to other files. Has the following child nodes:
      

      class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy": View the size of the current active file, if it exceeds the specified size, it will tell RollingFileAppender to trigger the rolling of the current active file. There is only one node:
        

      

logger

Used to set the log printing level of a package or a specific class, and specify

root

it is also

Guess you like

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