preface
The previous chapter explained simple integration of Spring Boot logs, where all logs are output to one file, but in a real project, we need to separate logs into different files, such as regular logs, exception logs, monitoring logs, and so on. Spring Boot logs are implemented by sf4J + Logback by default. The default configuration file is logback-spring. XML. If you want to output the logs to different files, you need to customize the logback. XML file.
logback.xmlProfile Definition
<? The XML version = "1.0" encoding = "utf-8"? > <configuration> <property name="LOG_PATTERN" value="%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" /> <property name="LOG_PATH" value="/logs"/> <! - the CONSOLE log format - > < appender name = "CONSOLE" class = "ch. Qos. Logback. Core. ConsoleAppender" > < encoder charset = "utf-8" > <pattern>${LOG_PATTERN}</pattern> </encoder> </appender> <! - the system INFO log - > < appender name = "SYS_INFO" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > <File>${LOG_PATH}/info.log</File> <append>true</append> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/info-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern> <maxHistory>15</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder charset="UTF-8"> <pattern>${LOG_PATTERN}</pattern> </encoder> </appender> <! - system EEOR level log - > < appender name = "SYS_ERROR" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > <File>${LOG_PATH}/error.log</File> <append>true</append> <! - the level of the error log - > < filter class = "ch. Qos. Logback. Classic. Filter. LevelFilter" > < level > error < / level > <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/error-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern> <maxHistory>15</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder charset="UTF-8"> <pattern>${LOG_PATTERN}</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="SYS_INFO"/> <appender-ref ref="SYS_ERROR"/> </root> </configuration>Copy the code
Configuration Description The preceding configuration defines info and error logs and outputs them to different log files. The size of each log file is 10 MB. It is recommended that the log file be saved for 15 days, so that some exceptions can occur every week.
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/error-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxHistory>15</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
Copy the code
Reference the custom logback.xml file
Configure the following information in your project’s application.yml file
logging:
config: classpath:logback.xml
Copy the code
Additional instructions
By default, the log configuration file is stored in the classpath path. After the project is packaged, the log configuration file is stored in the JAR package. If you need to modify the log file information (for example, change the storage path), you need to package the log configuration file again, which is inconvenient. Import the external logback.xml file
logging:
config: file:config/logback.xml
Copy the code
File: relative path of a file.
This article focuses on general logs and exception logs. The next section explains monitoring log output.