SpringBoot is designed to simplify the creation, running, debugging, deployment and other problems of Spring applications. The auto-assembly feature allows us to better focus on the business itself rather than external XML configuration. We only need to follow the specification and introduce relevant dependencies to easily build a WEB project
Spring Boot uses Commons Logging internally for Logging, but provides default configurations for Java Util Logging, Log4J2, Logback, and other Logging frameworks underneath.
There are a number of logging frameworks available in Java, but don’t worry; in general, the Default Logback for SpringBoot will do.
Log format
The default output log format of SpringBoot is as follows:
2014-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.052.2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
2014-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2014-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/ *]Copy the code
Output the following elements:
Logback
There is noFATAL
Level of logging, which will be mapped toERROR
- Time and date: accurate to the millisecond and can be used for sorting
- The log level can be ERROR, WARN, INFO, DEBUG, or TRACE
- The process ID
- Delimiter: used
---
To identify the beginning of the log - Thread name: enclosed in square brackets (may truncate console output)
- Logger name: The name of the class that normally uses the source code
- Log content: The message we output
Log output
By default, the log levels of SpringBoot are INFO, WARN, and ERROR. If you need to output more logs, you can enable SpringBoot in the following ways
- Command mode configuration:
java -jar app.jar --debug=true
, such orders will beSpringBoot
Parse with the highest priority - Resource file configuration:
application.properties
configurationdebug=true
Can.This configuration only works for embedded containers, Spring, Hibernate, and our own project would like to outputDEBUG
Additional configuration is required (Configuration rules:logging.level.<logger-name>=<level>
)
The log output level is configured
Logging. Level. Root = WARN logging.level.org.springframework.web = DEBUG logging.level.org.hibernate = # ERROR such as mybatis SQL log logging.level.org.mybatis = INFO logging. Level. The mapper is package = DEBUGCopy the code
Configure the log output format
- Logging.pattern. console: Define the format for output to the console (JDK Logger not supported)
- Logging.pattern.file: Defines the format for output to a file (JDK Logger is not supported)
Color coding
If the end supports ANSI, by default to log on a color, readability, spring can be set in the configuration file. The output. The ANSI. Enabled to change the default value
- ALWAYS:To enable the
ANSI
Color output. - DETECT:Try to detect
ANSI
Whether the coloring function is available. - NEVER:disable
ANSI
Color output.
Code comparison table
Level | Color |
---|---|
WARN |
Yellow |
FATAL ,ERROR |
Red |
INFO ,DEBUG ,TRACE |
Green |
If you want to change the log default color value, you can convert it by using the % CLR keyword. For example, if you want the text to be yellow % CLR (%d{YYYY-MM-DD HH: MM: ss.sss}){yellow}. Currently supported colors are blue, Cyan, faint, Green, Magenta, Red, and yellow.
file
By default, SpringBoot only prints logs to the console, not to log files. If you want to write log files in addition to console output, you need to set the logging.file or logging.path property in application.properties.
- Logging.file: Writes logs to a specified file. The default path is relative and can be set to absolute
- Logging. The path:Will be called
spring.log
Writes to the specifiedfolderSuch as,/var/log
)
Log files are cut when they reach 10MB, and a new log file (such as spring.1.log and spring.2.log) is generated. The new log file is still output to spring.log. By default, ERROR, WARN, and INFO messages are recorded.
- Logging.file. max-size: limits the log file size
- Logging.file. max-history: limits the number of log retention days
Custom log configuration
Since logs are initialized before ApplicationContext, SpringBoot provides us with the logging.config property to configure custom log files. By default it is automatically loaded based on log dependencies.
Logging System | Customization |
---|---|
JDK (Java Util Logging) |
logging.properties |
Log4j2 ,ERROR |
Log4j2 – spring. XML or log4j2. XML |
Logback |
Groovy logback.xml logback.groovy logback.xml logback.groovy |
Logback extended configuration
This extension configuration only applies to files with logback-spring. XML or logging.config properties, because logback. XML loads too early to obtain some of SpringBoot’s extended properties
Using the extended properties springProfile and springProperty makes your logback-spring. XML configuration more challenging, while others are still struggling to get logback-{profile}.xml in one file…
springProfile
The
tag allows us to make the configuration file more flexible by selectively including or excluding certain configurations.
<springProfile name="dev">
<! -- Enabled while developing the environment -->
</springProfile>
<springProfile name="dev,test">
<! -- Activated during development and testing -->
</springProfile>
<springProfile name=! "" prod">
<! -- This configuration is not activated in a "production" environment -->
</springProfile>
Copy the code
case
<! -- Development environment log level is DEBUG/ and development environment does not write log files -->
<springProfile name="dev">
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>
</springProfile>
<! -- Test environment log level is INFO/ and log file -->
<springProfile name="test">
<root level="INFO">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</root>
</springProfile>
Copy the code
springProperty
The
tag allows us to use properties in the Spring Environment in Logback. This is a good solution if you want to read back the value of the application.properties configuration in logback-spring.xml
<! Scope: scope name: key used in logback-spring. XML source: The key defaultValue in the application.properties file: default -->
<springProperty scope="context" name="logName" source="spring.application.name" defaultValue="myapp.log"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/${logName}.log</file>
</appender>
Copy the code
conclusion
For more details please refer to the official documentation: docs.spring. IO /spring-boot…
This tutorial is based on the latest spring-boot-starter-parent: 2.0.1.RELEASE, including the new version features will be introduced together…
Say something
- Personal QQ: 1837307557
- Battcn Open Source Group (for beginners) : 391619659
- Wechat official account (welcome to flirt) :
battcn
Personal blog: blog.battcn.com/