A preface
Knowledge seeker Springboot series logback, think the article is good, like plus attention thank you; Pay attention to the public number of knowledge seekers to get more wonderful original content;
Logback is a mature Log4j project created by Ceki Gulcu, creator of the Log4J logging framework.
The default logging framework used by SpringBoot is LogBack, which consists of three components
- logback-core
- logback-classic
- logback-access
The logback-spring. XML file is automatically loaded in the classpath (Resource directory)
Logback Basic properties
<configuration debug="true"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <! -- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="STDOUT" /> </root> </configuration>Copy the code
Log Format description
- %d represents the date and time
- %thread indicates the name of the thread
- %-5level: level displays 5 character widths from the left
- Logger: The class name that is usually used in the source code
- % MSG: log message
- %n is a newline character
The valid log levels are as follows
level of request p | ||||||
---|---|---|---|---|---|---|
TRACE | DEBUG | INFO | WARN | ERROR | OFF | |
TRACE | YES | NO | NO | NO | NO | NO |
DEBUG | YES | YES | NO | NO | NO | NO |
INFO | YES | YES | YES | NO | NO | NO |
WARN | YES | YES | YES | YES | NO | NO |
ERROR | YES | YES | YES | YES | YES | NO |
2.1 Configuration Label attributes
- Scan: The configuration file will be reloaded if it is changed. The default value is true
<configuration debug="true">
...
</configuration>
Copy the code
- Debug: Displays the logback running status in real time. The default value is false
<configuration scan="true">
...
</configuration>
Copy the code
- ScanPeriod: indicates the interval for checking whether the configuration file is modified. The default value is every minute. Readers can set up examples
30 seconds
.30 minutes
.3 hours
<configuration scan="true" scanPeriod="30 seconds" >
...
</configuration>
Copy the code
- PackagingData: Whether to enable packing data in the stack trace. The default is false.
<configuration packagingData="true">
...
</configuration>
Copy the code
2.2 statusListener label
StatusListener is a child element of configuration. This is called a state listener, and at the top of the sub-tab of configuration, it listens for events.
<configuration>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
... the rest of the configuration file
</configuration>
Copy the code
2.3 the property tag
The name, value attribute is used to define the name and value of a variable. In this context, it can be called as ${name}
<configuration>
<property name="USER_HOME" value="/home/sebastien" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/myApp.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
Copy the code
If the following example is defined, the configuration information will be read in variables1.properties
<configuration>
<property file="src/main/java/chapters/configuration/variables1.properties" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/myApp.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
Copy the code
Variables1. Properties:
USER_HOME=/home/sebastien
Copy the code
2.4 appender label
Appenders are children of Configuration. Each appender is a logging component that can define one type of logging.
name
: The name of the appender, which is mainly usedref
.class
: Defines the appender component.scope
: Specifies the scope;LOCAL
.CONTEXT
.SYSTEM
The following is an example: Define two components, one is file storage, one control output, through the root tag reference can take effect at the same time;
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>myApp.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
Copy the code
2.5 contextName label
Child element of contextNameconfiguration. Each logger can be bound with a contextName. The default contextName is default, which cannot be changed once it is set.
<configuration>
<contextName>myAppName</contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %contextName [%t] %level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Copy the code
2.6 the logger label
Used to set logging output for a package or a specific class and specify
. The name attribute is an optional, level, addtivity (whether to pass prints to the parent loger) attribute
If you don’t want to see debug level logs in package Chapters.Configuration, as shown below, do the following
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <! -- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="chapters.configuration" level="INFO"/> <! -- Strictly speaking, the level attribute is not necessary since --> <! -- the level of the root level is set to DEBUG by default. --> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> </configuration>Copy the code
Print the following
17:34:07. 578. [the main] INFO chapters. Configuration. MyApp3 - if application. 17:34:07. [the main] 578 INFO chapters.configuration.MyApp3 - Exiting application.Copy the code
2.7 the root tag
The root tag is essentially a
tag, but it’s the root tag; If the
or
tag is set to the output level, it inherits that level by default!
<! <root level="INFO"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root>Copy the code
2.8 the include tag
Contains configuration information about other files
<configuration>
<include file="src/main/java/chapters/configuration/includedConfig.xml"/>
<root level="DEBUG">
<appender-ref ref="includedConsole" />
</root>
</configuration>
Copy the code
Includedconfig. XML example, which must contain the
tag
<included>
<appender name="includedConsole" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>"%d - %m%n"</pattern>
</encoder>
</appender>
</included>
Copy the code
If is the URL
<include url="http://some.host.com/includedConfig.xml"/>
Copy the code
Configure multiple environments
To support non-conflicting logbacks in development, testing, and production environments,
, then can be configured to make the target environment effective.
Format is as follows
<! -- if-then form --> <if condition="some conditional expression"> <then> ... </then> </if> <! -- if-then-else form --> <if condition="some conditional expression"> <then> ... </then> <else> ... </else> </if>Copy the code
Four examples
4.1 Example of a Normal Log appender
<! Logappender: According to the daily log FILE -- > < appender name = "NORMAL - the FILE" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > <append>true</append> <! - the name of the log - > < file > ${logging. The path} / ZSZXZ - boot/ZSZXZ - error. The log < / file > <! - a day to generate a log file, save 30 days of the log file - > < rollingPolicy class = "ch. Qos. Logback. Core. Rolling. TimeBasedRollingPolicy" > <! Path}/zszxz-boot/ zszz.log.%d{YYYY-MM-DD}</FileNamePattern> <! -- Log file retention days --> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: -> <pattern>%d{YYYY-MM-DD HH: MM :ss.SSS} [%thread] %-5level % Logger {50} - % MSG %n</pattern> <! Utf-8 </charset> </encoder> </appender>Copy the code
4.2 Error Log appender Example
<! Error logging appender: According to the daily log FILE -- > < appender name = "ERROR - the FILE" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > <append>true</append> <! -- filter, Only record the error level log - > < filter class = "ch. Qos. Logback. Classic. Filter. ThresholdFilter" > < level > error < / level > < / filter > <! - the name of the log - > < file > ${logging. The path} / ZSZXZ - boot/ZSZXZ - error. The log < / file > <! - a day to generate a log file, save 30 days of the log file - > < rollingPolicy class = "ch. Qos. Logback. Core. Rolling. TimeBasedRollingPolicy" > <! ${logging.path}/zszxz-boot/zszxz-error.log.%d{YYYY-MM-DD}</FileNamePattern> <! -- Log file retention days --> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: -> <pattern>%d{YYYY-MM-DD HH: MM :ss.SSS} [%thread] %-5level % Logger {50} - % MSG %n</pattern> <! Utf-8 </charset> </encoder> </appender>Copy the code
V. Official Documents
For more information about log configuration, see the official documentation
Logback. Qos. Ch/manual/inde…
This set of tutorials
- Springboot introduction (1)
- Springboot Custom banner(2)
- Springboot configuration file parsing (3)
- Springboot integration mybatis (4)
- JdbcTemplate springboot integration (5)
- Spingboot Unit Test (6)
- Springboot integration thymeleaf (7)
- Springboot Multi-file upload (8)
- Springboot file download (9)
- Springboot Custom exception Class (10)
- Springboot Multi-environment Configuration (11)
- Springboot Automatic configuration principle (12)
- Springboot integrated with restTemplate interface call (13)
- Springboot Integrated Task Scheduling (14)
- Springboot Cross-domain CORS Processing (15)
- Springboot enables GIZP compression (16)
- Springboot integration logback (17)
- Springboot integration Swagger (18)
- Springboot Integrated Actuator Monitoring (19)
- Springboot integration mybatis + oracle + druid (20)
- Springboot integration springsession (21)
- JWT springboot integration (22)
- Springboot integration with admin background Monitoring (23)
- Springboot Integration redis Basics (24)
- Redis Cache with SpringBoot
- Springboot uses AOP log interception (26)
- Springboot integration Validation (27)
- Springboot integration mybatisPlus (28)
- Springboot integration shiro (29)
- Springboot implementation of interface parity check (30)
- Springboot – integrated web sockets (31)
- RestTemplate (32)
- SpringBoot uses @async asynchronous calls with thread pools (33)
- To be continued