1.logback Composition of configuration file
1.1 rootnode
The root node is a required node to specify the most basic level of log output, with only one level attribute to set the print level. The following are optional: TRACE,DEBUG,INFO,WARN,ERROR,ALL,OFF.
root nodes can contain 0 or more elements, adding appender s.The following:
<root level="debug"> <appender-ref ref="console" /> <appender-ref ref="file" /> </root>
appender is also one of the child nodes, which will be explained later.
1.2.property node
Used to define variables for easy use.There are two attributes: name,value.After defining variables, you can use ${} to work with them.The following:
<property name="path" value="./log"/> <property name="appname" value="app"/>
1.3.appender node
There are two required attributes in appender--name and class.Name is the name of the node and class is the fully qualified class name, which is the processing class for the log output destination.Where the encoder subnode controls the output format of the log
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern> </encoder> </appender>
Most important is the node appender uses to format the log output.There are two properties:
- name: This appender is named
- class: Specifies the output policy, typically in two ways: console output, file output
Here's an example of how this works:
There are three main log destination processing classes in logback:
1.3.1 ch.qos.logback.core.ConsoleAppender
Output log to console
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern> </encoder> </appender>
1.3.2 ch.qos.logback.core.FileAppender
You can either specify a specific location or format the output of the log to a specific disk file.
<appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>e:/log.out</file> <append>true</append> <prudent>false</prudent> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender>
Child node append: Whether the new log is written to as appended to the end of the fileLog.outIn the file, true is appended and fasle is written to empty the existing file;
Child node prudent: Whether the log is safely written to a disk file, defaulting to false.If true, it is inefficient;
1.3.3 ch.qos.logback.core.rolling.RollingFileAppender
Scroll through the log and move the existing log to a new file when the conditions set in the rolling Policy node are met.The conditions that can be set in rollingPolicy node are: file size, time, etc.
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>e:/log.out</file> <append>true</append> <prudent>false</prudent> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>testLog-%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender>
1.3.4 ch.qos.logback.classic.AsyncAppender
Asynchronous logging, internal use of caching to achieve asynchronous printing, log printing event s into the cache.The specific data structure is BlockingQueue;
<appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>e:/log.out</file> <append>true</append> <prudent>false</prudent> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <appender name ="ASYNC" class= "ch.qos.logback.classic.AsyncAppender"> <discardingThreshold>0</discardingThreshold> <queueSize>512</queueSize> <appender-ref ref ="FILE"/> </appender>
Child node queueSize: refers to the queue size of BlockingQueue, which defaults to 256;
discardingThreshold: If 20% capacity is left in BlockingQueue, the program discards TRACE, DEBUG, and INFO-level log print event, leaving only WARN and ERROR-level events.To preserve all log print events, you can set the value to 0.
1.3.5 rollingPolicy
The scrolling policy of the log file, used in conjunction with the RollingFileAppender, determines the behavior of the RollingFileAppender when the log file changes.There is a class attribute in the rolling Policy node, and the optional values are TimeBasedRollingPolicy, Fixed WindowRollingPolicy, TriggeringPolicy.The ch.qos.logback.core.rolling.TimeBasedRollingPolicy represents the rolling strategy for log files based on time.
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>testLog-%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy>
Ch.qos.logback.core.rolling.Fixed WindowRollingPolicy indicates that if the log file size exceeds the specified range, it will be split into multiple files based on the file name;
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>tests.%i.log.zip</fileNamePattern> <minIndex>1</minIndex> <maxIndex>4</maxIndex> </rollingPolicy>
2. Examples of configuration files
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!--Set Storage Path Variable--> <property name="LOG_HOME" value="./log"/> <!--console output appender--> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <!--Format Output--> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--Format output:%d Indicates the date,%thread Represents the thread name,%-5level: Level shows 5 character width from left%msg: Log messages,%n Is a line break--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> <!--Set Encoding--> <charset>UTF-8</charset> </encoder> </appender> <!--File Output,Time window scrolling--> <appender name="timeFileOutput" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--Log Name,Specify the latest filename, other filenames use FileNamePattern --> <File>${LOG_HOME}/timeFile/out.log</File> <!--File Scroll Mode--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--File name of log file output,Settable file type is gz,Turn on file compression--> <FileNamePattern>${LOG_HOME}/timeFile/info.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern> <!--Days of log file retention--> <MaxHistory>30</MaxHistory> <!--Divide the same day by size--> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <!--Output Format--> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--Format output:%d Indicates the date,%thread Represents the thread name,%-5level: Level shows 5 character width from left%msg: Log messages,%n Is a line break--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> <!--Set Encoding--> <charset>UTF-8</charset> </encoder> </appender> <!-- Asynchronous Output --> <appender name="ASYNC-INFO" class="ch.qos.logback.classic.AsyncAppender"> <!-- Do not lose logs.Default,If Queue 80%already expired,Will discard TRACT,DEBUG,INFO Level Log --> <discardingThreshold>0</discardingThreshold> <!-- Change the default queue depth,This value can affect performance.Default value is 256 --> <queueSize>256</queueSize> <!-- Add Additional appender,Only one can be added at most --> <appender-ref ref="timeFileOutput"/> </appender> <!--Specify the underlying log output level--> <root level="INFO"> <!--appender Will be added to this loger--> <appender-ref ref="console"/> <appender-ref ref="timeFileOutput"/> </root> </configuration>
3 spring boot uses logback logs
The SpringBoot project has its own logback and slf4j dependencies, so simply add spring-boot-starter-logging to the maven dependencies.The logback framework loads a configuration file named logback-spring or logback under classpath by default.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
3.1 Simple use
If you do not need complex log configurations and perform a simple set of log printing levels, the printing method can be configured directly in application.yml.
logging: # Configure the log storage path with the log file name: spring.log path: e:/log file: # Set log file size max-size: 20MB level: # root log output at info level root: info # All class es under this package are output at the DEBUG level com.demo.test: debug
3.2 Multi-environment Log Output
The official recommendation for Spring Boot is to use a file name with-spring as your log configuration (for example, logback-spring.xml instead of logback.xml) and a log configuration file named logback-spring.xml, for which spring boot can add some examples of spring boot-specific configurations:
<configuration> <!-- testing environment+development environment. Multiple separated by commas. --> <springProfile name="test,dev"> <logger name="com.test.demo" level="DEBUG" additivity="false"> <appender-ref ref="console"/> </logger> </springProfile> <!-- production environment. --> <springProfile name="prod"> <logger name="com.test.demo" level="INFO" additivity="false"> <appender-ref ref="timeFileOutput"/> </logger> </springProfile> </configuration>