In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to configure the springboot logback log framework". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Logback can configure logs not only through application configuration files, but also through logback-spring.xml. In general, using the global configuration file application.yml or properties for configuration is sufficient, and if your log output requirements are particularly complex and personalized, you can consider using logback-spring.xml configuration.
1. Application configuration file to implement log configuration
We can configure the log in the applicaition.properties (yml) file
Logging: level: root: info com.zimug.boot.launch.controller: debug file: path: d:\ logs name: d:\ logs\ boot-launch.log max-size: 10MB max-history: 10 pattern: console:'% red (% d {yyyy-MM-dd HH:mm:ss})% green ([% thread])% highlight (%-5level)% boldMagenta (% logger {10}) -% cyan (% msg%n) ) 'file:% d {yyyy-MM-dd HH:mm:ss.SSS}%-5level [% thread]% logger:% msg%n'
Logging.level.root=info specifies that the default log level for the entire system is info, and the log level is unified.
Logging.level.com.zimug.boot.launch.controller=debug, which specifies that the log level of a particular package is debug, and the log level is personalized. From the point of view of priority, personality configuration is larger than unified configuration.
Logging.file.path outputs the log to the specified directory, if not specified
Logging.file.name, the default name of the log file is spring.log. After logging.file.name is configured, the logging.file.path configuration is invalid.
Regardless of the setting, Spring Boot automatically splits the log files on a daily basis, that is, a new log file is automatically generated every day, while the previous one is automatically typed into a GZ package. # Log file size
You can set the maximum file capacity for each log split by logging.file.max-size=10MB, and the logs will continue to be separated beyond this size.
You can set the reserved log time logging.file.max-history=10, in days
The format of logs exported by logging.pattern.file to a file
The format of the logging.pattern.console console output log, which adds color to the log in order to display the log more clearly when debugging in the console. Red, green, etc.
Log format placeholder
With this diagram, take a look at the relationship between placeholders and logging.pattern.console format configuration
% d {HH:mm:ss.SSS}: log output time (red)
% thread: the process name of the output log, which is useful in Web applications and asynchronous task processing (green)
%-5level: log level and left-aligned with 5 characters (highlight highlight blue)
% logger: name of the log output class (boldMagenta bold magenta)
% msg: log message (cyan turquoise)
% n: newline character for the platform
Second, use logback-spring.xml to implement log configuration 2.1. Demand
In general, using the global configuration file application.yml or properties for configuration is sufficient, and if your log output requirements are particularly complex, consider using logback-spring.xml for configuration.
Spring boot prints logs with its own logback and prints in multiple environments:
The production environment is output to the console and files, one file a day, retained for 30 days.
The development environment outputs to the console and prints the sql (mybatis) output, but the production environment does not print this information
The test environment is output only to the console. Do not export to file
To print the Mybatis SQL, you can print the SQL simply by adjusting the log level of the package used to the Mybatis to DEBUG.
2.2. Demand realization
Since logback is the default logging framework for spring boot, there is no need to introduce maven dependency, just go to logback-spring.xml and put it under resources.
${CONSOLE_LOG_PATTERN} ${LOG_PATH}.% d {yyyy-MM-dd} .log 30% d {yyyy-MM-dd HH:mm:ss.SSS} [% thread]%-5level% logger {50} -% msg%n 10MB 0 500
Asynchronous log configuration:
The default value of asynchronous log queueSize is 256, which is the capacity of asynchronous log queue.
DiscardingThreshold: when the remaining capacity of the asynchronous log queue is less than this threshold, logs at TRACE, DEBUG or INFO level will be discarded. If you do not want to discard the log (that is, save it in full), you can set it to 0. But when the queue is full, the non-blocking asynchronous log becomes a blocked synchronous log. Therefore, the discardingThreshold discard policy can be set for unimportant logs in systems with high concurrency and low latency requirements, with a value greater than 0.
2.3. Test it
After the above configuration is complete, you can use the following code to test whether the requirements proposed in Section 2.1 are met.
Import org.slf4j.Logger;import org.slf4j.LoggerFactory;@RestControllerpublic class LogTestController {private static final Logger logger = LoggerFactory.getLogger (LogTestController.class); @ GetMapping ("/ testlog") public void test () {logger.trace ("Trace log..."); logger.debug ("Debug log..."); logger.info ("Info log..."); logger.warn ("Warn log...") Logger.error ("Error log...");}} "how to configure the springboot logback log framework" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.