This is the 12th day of my participation in the First Challenge 2022
SpringBoot supports Java Util Logging, Log4J, Log4J2, and Logback Logging frameworks. By default, Logback Logging is used. In actual SpringBoot projects, the default SpringBoot log configuration cannot meet actual production and development requirements. You need to select an appropriate log output framework and flexibly adjust the log output level and log output format. This chapter describes how to configure logs for the SpringBoot project.
(Logback logging configuration is strongly recommended, as it is much more efficient than Log4J.)
1. Add log dependency packages
The dependency package spring-boot-starter in the SpringBoot project already contains spring-boot-starter-logging. This dependency package is the default logback logging framework and therefore does not need to be imported.
2. Add logback.xml
In/springboot/SRC/main/resources directory, new log logback configuration file. The XML, as follows:
(See note for configuration instructions)
<! -- scan: When this property is set to true, the configuration file will be reloaded if any changes occur. The default value is true. ScanPeriod: Sets the interval at which the configuration file is monitored for changes. The default interval is 1 minutebug:When this property is set to true, internal logback logs are displayed to check the running status of logback in real time. The default value is false -->
<configuration scan="true" scanPeriod="2 seconds">
<! RollingFileAppender: RollingFileAppender can be used to scroll a file and log to a specified file and, when a condition is met, to another RollingFileAppender Class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" parameters: <append>: true if the log is appended to the end of the file, false if the existing file is emptied, the default is true. <file>: The name of the file to be written to, which can be a relative or absolute directory. If the parent directory does not exist, it is automatically created. There is no default <rollingPolicy>: determines the behavior of the RollingFileAppender when scrolling occurs, Involving file movement and renaming <triggeringPolicy>: Tells RollingFileAppender to properly enable scrolling > > > FixedWindowRollingPolicy supports TimeBasedRollingPolicy, but has two limitations :1 file compression is not supported or allowed,2 file property cannot be set and must be left empty -->
<appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<! If true, the log is appended to the end of the file, if false, the existing file is emptied. The default is true -->
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<! Log files that scroll once a day are saved only within 30 days.
<fileNamePattern>logs/%d{yyyy-MM-dd}/springboot_%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<! -- Format the log -->
<pattern>%date %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/%d{yyyy-MM-dd}/springboot-error_%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<! If the log level is the same as the configuration level, the filter will accept or reject the log parameters based on onMath and onMismatch. <level>: sets the filtering level. <onMatch>: sets the operations that meet the filtering conditions. <onMismatch>: Sets the operations that do not meet the filtering conditions.
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<! - the role definition console appender: the log output to the console class = "ch. Qos. Logback. Core. ConsoleAppender" -- >
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date %level [%thread] %logger{10}.%class{0}#%method[%file:%line] %n%msg%n</pattern>
</layout>
</appender>
<! Set root to "error" and specify "console","fileAppender","errorAppender" appender -->
<root level="error">
<appender-ref ref="console"/>
<appender-ref ref="fileAppender"/>
<appender-ref ref="errorAppender"/>
</root>
<! Logger sets the log level <loger> for a package with only a name attribute, an optional level, and an optional addtivity attribute name that specifies a package or a specific class bound by this loger Level: Sets the print level, case-independent :TRACE, DEBUG, INFO, WARN, ERROR, ALL, and OFF addtivity: whether to convey print messages to the parent loger. The default is true, which inputs information to the root configuration. Multiple appender-refs can be included, indicating that the appender will be added to the logger.
<logger name="com.xcbeyond.springboot" level="debug"/>
</configuration>
Copy the code
3. Log printing.
This section describes how to output debug logs in the startup class of a project.
package com.xcbeyond.springboot; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** ** SpringBoot boot class * @author xcbeyond */ @springBootApplication public class SpringBootApplication { private static Logger logger = LoggerFactory.getLogger(SpringbootApplication.class); public static void main(String[] args) { if(logger.isDebugEnabled()) { logger.debug("SpringBoot starting..." ); } SpringApplication.run(SpringbootApplication.class, args); } } import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** ** SpringBoot boot class * @author xcbeyond */ @springBootApplication public class SpringBootApplication { private static Logger logger = LoggerFactory.getLogger(SpringbootApplication.class); public static void main(String[] args) { if(logger.isDebugEnabled()) { logger.debug("SpringBoot starting..." ); } SpringApplication.run(SpringbootApplication.class, args); }}Copy the code
Note: Please use the package org.slf4j.Logger, org.slf4j.LoggerFactory. SLF4J is only a logging standard, not a specific implementation of the logging framework. For later maintenance, you can configure different types of logging according to different logging frameworks without modifying the log output code.
4. Start the project.
The logs\2018-07-11\ log folder and log files are generated in the sibling directory of the project.