Commons Logging in all internal logs, but it is open to the implementation of the underlying log. In the Spring Boot ecosystem, Java Util Logging , Log4J2 and Logback provide automated configuration components. Each Logger can be configured to output log content in the console or in a file. By default, when we use various Starters, we will use Logback to implement log management.
How to keep a log
There are many ways to write logs. I will not list all the ways here, only the most used way of DD!
First of all, at the code level, we do not entangle whether to use the default Logback or Log4j, but directly use: Slf4j .
Why don't you need to use Logback or Log4j to write code? This is the benefit of using Slf4j, why Slf4j? Full English name: Simple Logging Facade for Java, namely: Simple Logging Facade. It is not a specific logging solution. The actual work is still a logging framework such as Logback or Log4j. Slf4j is a typical application case of facade mode among 23 design modes. Through the abstract existence of such a facade of Slf4j, after writing the code, we only rely on this abstract log operation, and the specific implementation will be delegated to the specific implementation when the Slf4j facade is called.
For example, the following is a simple logging example:
@Slf4j
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
log.error("Hello World");
log.warn("Hello World");
log.info("Hello World");
log.debug("Hello World");
log.trace("Hello World");
}
}
Note: Here we pom.xml
, and then use @Slf4j
declare to introduce Slf4j's log
logging object, and then you can easily use it to log. How the log is written to the console or file is determined by the specific logging framework introduced in the Spring Boot project. By default, it is Logback.
Log element
Starting any Spring Boot project, we can see a lot of log information in the console, such as the following log:
There are a total of 7 elements in the output content of the log, which are as follows:
- Time and date: accurate to the millisecond
- Log level: ERROR, WARN, INFO, DEBUG or TRACE
- Process id
- Separator:
---
identifies the beginning of the actual log - Thread name: enclosed in square brackets (may be truncated console output)
- Logger name: usually use the class name of the source code
- Log content
Log output
In Spring Boot applications, logs will be output to the console by default. The default output log levels include: ERROR
, WARN
and INFO
. We can run the Hello World example written above to verify this default setting:
2021-12-28 17:37:25.578 INFO 65136 --- [ main] com.didispace.chapter81.Application : Started Application in 2.695 seconds (JVM running for 3.957)
2021-12-28 17:37:25.579 ERROR 65136 --- [ main] com.didispace.chapter81.Application : Hello World
2021-12-28 17:37:25.579 WARN 65136 --- [ main] com.didispace.chapter81.Application : Hello World
2021-12-28 17:37:25.579 INFO 65136 --- [ main] com.didispace.chapter81.Application : Hello World
open DEBUG log
We can switch to the DEBUG
level in two ways:
first type --debug
flag after running the command, such as: $ java -jar myapp.jar --debug
second : in the configuration file application.properties
configuration debug=true
The DEBUG log opened here only affects the core Logger, and more content will be output at the framework level including embedded container, hibernate, spring, etc., but the log of your own application will not be output to the DEBUG level. From the screenshot below, we As you can see, the debug level Hello written by ourselves
World did not output.
Log configuration
Here are some commonly used log configurations to help us better manage the log content.
Colorful output
If your terminal supports ANSI, setting the color output will make the log more readable. It is application.properties
by setting the spring.output.ansi.enabled
parameter in 061d533eb5df0b, which has three options:
- NEVER: Disable ANSI-colored output
- DETECT: Will check whether the terminal supports ANSI, if yes, use color output (default item)
- ALWAYS: Always use ANSI-colored format for output, if the terminal does not support it, there will be a lot of interference information, it is not recommended to use
Note: In Spring Boot 1.x, the default value is NEVER, after 2.x it defaults to DETECT, so look at our screenshot above, the default is already colored. So if you are a Spring Boot 2.x version user, this basically does not need to be modified.
File output
The default configuration of Spring Boot will only be output to the console and will not be recorded in a file, but we usually need to record it in a file when we use it in a production environment.
To increase file output, you need to application.properties
, such as this:
logging.file.name=run.log
logging.file.path=./
logging.file.name
: Set the file namelogging.file.path
: Set the file path
Note: This is different from the 1.x version. In 1.x, the corresponding parameters arelogging.file
andlogging.path
.
File scrolling
It is obviously inappropriate to output the log in a file all the time, and any log framework will prepare the rolling configuration of the log file for this. Since this article will be configured by default, it is the configuration of Logback. There are several specific ones:
logging.logback.rollingpolicy.file-name-pattern
: The file name pattern used to create log files.logging.logback.rollingpolicy.clean-history-on-start
: Whether to clean up the log archive when the application starts, the default is false, no cleanuplogging.logback.rollingpolicy.max-history
: The maximum number of archived log files to keep, the default is 7logging.logback.rollingpolicy.max-file-size
: The maximum size of the log file before archiving, the default is 10MBlogging.logback.rollingpolicy.total-size-cap
: The maximum size of the log file before it is deleted, the default is 0B
Level control
If you want to do some simple output level control for each Logger, you only need to configure application.properties
Configuration format: logging.level.*=LEVEL
logging.level
: Log level control prefix,*
is the package name or Logger nameLEVEL
: Options TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
For example:
logging.level.com.didispace=DEBUG
:com.didispace
package are output with DEBUG levellogging.level.root=WARN
: root log output at WARN level
After doing this configuration, you can execute the above program again, and the original debug-level Hello World can be successfully output.
Custom log configuration
Since the log service is generally initialized before the ApplicationContext is created, it does not have to be controlled by the Spring configuration file. Therefore, log control and management can still be well supported through system properties and traditional Spring Boot external configuration files.
According to different log systems, you can organize the configuration file name according to the following rules, and it will be loaded correctly:
- Logback:
logback-spring.xml
,logback-spring.groovy
,logback.xml
,logback.groovy
- Log4j2:
log4j2-spring.xml
,log4j2.xml
- JDK (Java Util Logging):
logging.properties
Spring Boot officially recommends using -spring
as your log configuration (for example, use logback-spring.xml
instead of logback.xml
)
Custom output format
In Spring Boot, the output format can be controlled by configuring the following parameters application.properties
- logging.pattern.console: Define the style of output to the console (JDK Logger is not supported)
- logging.pattern.file: defines the style of output to a file (JDK Logger is not supported)
Well, that's all for today's study! If you encounter difficulties in the learning process? You can join our super high-quality Spring technical exchange group , participate in exchanges and discussions, and learn and progress better! The summary page "Spring Boot Basic Tutorial" can be clicked directly! , welcome to collect and forward support!
Code example
The complete project of this article can be viewed in chapter8-1
project 2.x
directory in the following warehouse:
- Github:https://github.com/dyc87112/SpringBoot-Learning/
- Gitee:https://gitee.com/didispace/SpringBoot-Learning/
If you think this article is good, welcome Star
support, your attention is my motivation for persistence!
Welcome to pay attention to my official account: Program Ape DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。