In Spring Boot projects, Logback is used by default as the log framework. Logback is an efficient and flexible logging tool that supports a variety of log formats and output targets (such as consoles, files, etc.). By configurationFile, you can customize the output method and format of the log.
The following is a detailed description of the Logback log configuration in Spring Boot:
1. Create or modify
In Spring Boot projects, the default is usedFile to configure logs. If the file does not exist, you can
src/main/resources
Create one in the directory.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- Configure console output --> <appender name="CONSOLE" class=""> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Configuration file output --> <appender name="FILE" class=""> <file>logs/</file> <rollingPolicy class=""> <fileNamePattern>logs/app-%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Configuration error log output --> <appender name="ERROR_FILE" class=""> <file>logs/</file> <filter class=""> <level>ERROR</level> </filter> <rollingPolicy class=""> <fileNamePattern>logs/error-%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Root log configuration --> <root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> </root> <!-- Log level for custom packages --> <logger name="" level="DEBUG" additivity="false"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> </logger> <!-- Configuration error log --> <logger name="" level="ERROR"> <appender-ref ref="ERROR_FILE"/> </logger> </configuration>
2. Configuration instructions
2.1 Log format
pass<encoder>
Tags can configure the output format of the log. Common placeholders include:
-
%d{yyyy-MM-dd HH:mm:}
: Date and time -
[%thread]
: thread name -
%-5level
: Log level (right aligned, width 5) -
%logger{36}
: Class name or package name (up to 36 characters) -
%msg
: Log messages
2.2 Console output
<appender name="CONSOLE" class="">
Configure console output and logs will be printed directly in the terminal.
2.3 File output
<appender name="FILE" class="">
File output supports scrolling strategy, and log files are generated by daily use:
-
logs/
: Current log file -
logs/
:Historical log file
Scrolling policy parameter description:
-
<fileNamePattern>
: Naming rules for historical log files -
<maxHistory>
: Keep 30 days of historical log
2.4 Error log output
<appender name="ERROR_FILE" class="">
Specially logging error-level logs, through<filter>
Tag settings record onlyERROR
Level log.
2.5 Root log configuration
<root level="INFO">
Set the level of the root log toINFO
and point the output target to the console and file.
2.6 Log level of custom package
pass<logger>
Tags can configure different log levels for specific packages or classes:
-
name=""
: Specify the package name -
level="DEBUG"
: Set the log level toDEBUG
-
additivity="false"
: means that the configuration of the parent logger is not inherited
3. Configure effective conditions
make sureThe file is located in
src/main/resources
In the directory. If the file is not in the project, Spring Boot uses the default log configuration.
4. Frequently Asked Questions
4.1 Log files are not generated
- examine
<file>
and<fileNamePattern>
Is the path correct? - Make sure the program has write permissions.
4.2 The log of the custom package does not take effect
- make sure
name
The attribute is exactly the same as the actual class name or package name. - make sure
additivity="false"
, avoid inheriting the configuration of the parent logger.
4.3 The log level is invalid
- make sure
<logger>
In the taglevel
Correct configuration (such asDEBUG
,INFO
,ERROR
)。 - Make sure no other configurations overwrite the log level.
5. Summary
Through the above configuration, you can flexibly define the log output method and format of Spring Boot applications. The log level, output target and file path can be adjusted according to actual needs, so as to better monitor and debug applications.
This is the end of this article about the detailed description of logback log configuration in springboot. For more related springboot log configuration content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!