In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to configure the logback log system in the Springboot project. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Logback depends on the jar package
The SpringBoot project configuration logback theoretically needs to add the logback-classic dependency jar package:
Ch.qos.logback logback-classic 1.2.3
However, because the SpringBoot project uses the logback log system by default, the spring-boot-starter or spring-boot-starter-web dependencies introduced when creating the SpringBoot project already contain spring-boot-starter-logging dependencies in the jar package, which also contains a variety of log system dependencies, as shown below:
Including logback and log4j, so you don't need to add additional dependencies, just configure logback.xml.
In addition, if you need to switch to log4j2, you need to exclude the commons-logging that comes with springboot in the spring-boot-starter-web dependency, and then introduce the dependent jar package of log4j2, as shown below:
Default configuration of org.springframework.boot spring-boot-starter-web commons-logging commons-logging org.springframework.boot spring-boot-starter-log4j2logback
As mentioned earlier, the SpringBoot project uses logback by default, so how does SpringBoot define the configuration of logback?
First, SpringBoot looks for logback-test.xml or logback.xml under the resource package, and if neither exists, it calls BasicConfigurator to create a minimized basic configuration.
The minimized configuration consists of a ConsoleAppender associated with the root logger, and the default output mode is% d {HH:mm:ss.SSS} [% thread]%-5level% logger {36} -% msg%n,root logger level is DEBUG, so no log files are generated, only output to the console.
Create a logback.xml profile
To control the log output by customizing the logback.xml configuration file, we usually configure three log components:
Console output
Output info level log files
Output error level log files
The following is the complete configuration of logback.xml
% d {yyyy-MM-dd HH:mm:ss.SSS}%-5level ${PID:-}-[% t]% logger {50} -% msg%n ${LOG_PATH} / info.log ${LOG_PATH} / info-%d {yyyy-MM-dd}.% i.log 30 100MB% d {yyyy-MM-dd HH:mm:ss.SSS}%-5level ${PID:-}-[% t]% logger {50} -% msg%n ERROR ${LOG_PATH} / error.log ${LOG_PATH} / error-%d {yyyy-MM-dd}.% i.log 30 100MB% d {yyyy-MM-dd HH:mm:ss.SSS}%-5level ${PID:-}-[% t]% logger {50} -% msg%n Logback configuration description Root Node configuration
Configuration contains the following three attributes:
Scan: overload when the configuration file changes. The default value is true.
ScanPeriod: monitor whether the configuration file is modified. The default value is 6000, and the default unit is millisecond.
Debug: print logback internal log information and check the running status of logback in real time. The default value is false.
Child node property:
Used to define the value of a variable, including the following two attributes
Name: variable name
Value: the value defined by the variable
The values defined by property are inserted into the logger context, allowing ${} to use variables, where the root directory of the log location is defined.
Child node appender:
Is the component responsible for writing the log, which controls the log output through a custom logging component, including the following two attributes:
Name: component name
Class: the class name specified by the component class
Different class corresponds to different functions:
For example, ch.qos.logback.core.ConsoleAppender will output the log to the console
Ch.qos.logback.core.rolling.RollingFileAppender outputs the contents of the log to the specified file
File node: the file name of the log file output
${LOG_PATH} / info.log
Filter node: filter that specifies the log output level of the log component, which takes precedence over the level of the root node.
ERRORrollingPolicy node:
Scrolling log file configuration, which involves moving and renaming log files, has only one class attribute, which is used to specify the scrolling policy. Here, ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy is used.
Contains the following three attributes:
FileNamePattern: how logs are named when scrolling occurs
MaxHistory: the maximum retention time of log files, which will be deleted automatically when the set time is exceeded
MaxFileSize: the maximum limit for each log file. When the limit is exceeded, the old log file will be regenerated and the old log file will be named according to the log naming method set by fileNamePattern.
${LOG_PATH} / error-%d {yyyy-MM-dd}.% i.log 30 100MB child node encoder% d {yyyy-MM-dd HH:mm:ss.SSS}%-5level ${PID:-}-[% t]% logger {50} -% msg%n
Formatting recorded events is responsible for two things, one is to convert the log information into a byte array, and the other is to write the byte array to the output stream.
PatternLayoutEncoder is the only useful and default encoder, with a node that sets the input format for the log. Using "%" plus "conversion character", if you want to output "%", you must escape "%" with "".
Child node root
The root loger, which is the highest level of all loger and has only one level node, uses class settings to print the log level, which defaults to debug, usually set to info, as well as trace, warn, error, all, and off levels.
The ref attribute of the child node appender-ref specifies the name of the log component, which is the value of the name attribute of appender.
Use logback
Add logging.config to the yml file to specify the address of the configuration file. If you name it logback, you don't need to configure it. SpringBoot` will find it automatically.
Level sets the log output level under the specified path.
Logging: config: classpath:logback.xml level: com: springboot: debug on "how to configure the logback log system in the Springboot project" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.