1. Place the logback-spring. XML log configuration file in resources

This document is on the web, the original author’s address github.com/xkcoding/sp…

<? xml version="1.0" encoding="UTF-8"? > <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> </filter> <encoder> <pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>  <charset>UTF-8</charset> </encoder> </appender> <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <! If you only want Info level logs, you will still get Error logs if you only filter Info level logs. Because of the high level of Error logs, you can use the following policy to avoid Error logs --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <! -- Filter Error--> <level> Error </level> <! --> <onMatch>DENY</onMatch> <! --> <onMismatch>ACCEPT</onMismatch> </filter> <! If there is both <File> and <FileNamePattern>, then the current log is <File>, and tomorrow it will automatically rename today's log to today's date. That is, the logs in <File> are from the current day. -- > <! --<File>logs/info.spring-boot-demo-logback.log</File>--> <! TimeBasedRollingPolicy--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <! File path, which defines how to split logs -- archive logs for each day into a file to prevent logs from filling the disk space --> <FileNamePattern>logs/info.created_on_%d{yyyy-MM-dd}.part_%i.log</FileNamePattern> <! <maxHistory>90</maxHistory> <! -- specifies the maximum size of a log file, at which old logs will be deleted --> <! --<totalSizeCap>1GB</totalSizeCap>--> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <! -- maxFileSize: This is the size of the active file. The default value is 10MB. Just for demo - > < maxFileSize > 2 MB < / maxFileSize > < / timeBasedFileNamingAndTriggeringPolicy > < / rollingPolicy > <! --<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> -- > <! --<maxFileSize>1KB</maxFileSize>--> <! --</triggeringPolicy>--> <encoder> <pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern> <charset>UTF-8</charset> <! </encoder> </appender> <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <! ThresholdFilter--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>Error</level> </filter> <! If there is both <File> and <FileNamePattern>, then the current log is <File>, and tomorrow it will automatically rename today's log to today's date. That is, the logs in <File> are from the current day. -- > <! --<File>logs/error.spring-boot-demo-logback.log</File>--> <! TimeBasedRollingPolicy--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <! File path, which defines how to split logs -- archive logs for each day into a file to prevent logs from filling the disk space --> <FileNamePattern>logs/error.created_on_%d{yyyy-MM-dd}.part_%i.log</FileNamePattern> <! - retain only the last 90 days of log - > < maxHistory > 90 < / maxHistory > < timeBasedFileNamingAndTriggeringPolicy class ="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <! -- maxFileSize: This is the size of the active file. The default value is 10MB. Just for demo - > < maxFileSize > 2 MB < / maxFileSize > < / timeBasedFileNamingAndTriggeringPolicy > < / rollingPolicy > < encoder > <pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern> <charset>UTF-8</charset> <! </encoder> </appender> <root level="info">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE_INFO"/>
        <appender-ref ref="FILE_ERROR"/>
    </root>
</configuration>Copy the code

2. If the logback configuration file is not available, check to see if the logback configuration file is not available under classes, and if it is not added to resource

      <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application-${profileActive}.yml</include>
                    <include>application.yml</include>
                    <include>templates/*.html</include>
                    <include>static/*.*</include>
                    <include>*.properties</include>
                    <include>*.xml</include>
                </includes>
                <filtering>true</filtering> <! - the default isfalse, the configuration oftrue, the @ references in the XML and properties files in the resource directory will be changed to configure @ andThe ${}--> </resource>Copy the code

3. How to use it in the project

Introducing the @slf4j log.info annotation ("Login user name:"+user.getUsername()+", password:+user.getPassword());
log.error("Login information cannot be empty!");Copy the code