In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of Log log integration in SpringBoot, which is very detailed and has certain reference value. Friends who are interested must finish it!
1. Classification of logs 1. Classification of names
Log4j: log for java (log4j because for and 4 pronunciation are similar)
LogBack log description
Note: springBoot integrates logback logs by default
2. Log classification
Root log: global log rootLogger (log level of springboot: info)
Sub-log: package log logger
3. Log level
OFF= "ERROR=" WARNING= "INFO (springboot default) =" DEBUG (mybatis default level) = "the higher the ALL level, the less the output information
Log level from low to high: the higher the log level, the less log information is output.
The log level uses the scenario DEBUGdebug level to record detailed information to facilitate debugging. In the production environment, we generally do not turn on DEBUGINFO to record the information of key code points, so that the code is executed as expected. The production environment usually sets the INFO level WARNING to record some unexpected situations. Such as insufficient disk ERROR information recorded when some functions cannot run properly due to a more serious problem CRITICAL when a serious error occurs, resulting in the application program cannot continue to run II. Introduction to logback
Logback is another open source logging component designed by the founder of log4j. Currently, logback is divided into three modules: logback-core,logback-classic and logback-access. It is a further improvement to the log4j log display.
1. Log classification in the project
Logs are divided into two categories
One is rootLogger: it is used to monitor all the running logs in the project, including those introduced in the dependent jar.
One is logger: it is used to listen for log information in the specified package in the project.
2. Used in java projects
(1) logback configuration file
The configuration file for logback must be placed in the project root directory and the name must be logback.xml
[% p]% d {yyyy-MM-dd HH:mm:ss} m% n
(2) use logs in specific classes
@ Controller@RequestMapping ("user") public class UserController {private Logger logger = org.slf4j.LoggerFactory.getLogger (this.getClass ()); @ RequestMapping ("findAll") public String findAll (String name) {logger.debug ("debug received name:" + name); logger.info ("info received name:" + name); logger.warn ("warn received name:" + name) Logger.error ("name received by error is:" + name); return "index";}}
(3) use the default log configuration
Logging: level: root: debug com.baizhi.dao: debug path: / Users/whj/aa.log file: bbb.log 3. Use 1 for logs in SpringBoot, and change the default level of Springboot to debug# to enable logs if level: off (less) > error > warn > info > debug (more) > alllogging: level: root: debug com.tjcu.controller: debug com.tjcu.dao: debug
Note: because springboot uses debug as the log to print too many things, it changes to info as the default log level, but mybatis and others use debug as the default log level, so we need to take into account his log isolation level
2. When the SpringBoot project starts, there will be a default log object: logger
Configure com.tjcu.controller: debug first
# enable log logging: level: root: debug com.tjcu.controller: debug
Use log objects in controller
@ Controller@CrossOrigin@ResponseBodypublic class EmpController {/ / define the log object private static final Logger log = LoggerFactory.getLogger (EmpController.class) in the control layer; @ Autowired private EmpService empService; @ RequestMapping ("/ emp/queryAll") public List queryall () {List emps = empService.showEmp (); for (Emp emp: emps) {log.debug (String.valueOf (emp));} return emps;}
After the front-end access
Http://localhost:8080/ems/emp/queryAll
Console results
Fourth, use the log plug-in in idea to simplify development. 1. Download the Log Support2 plug-in
2. Restart idea
3. A Log Support directory will be added to Settings.
FrameWork Framework Select slf4j
Why do we use slf4j?
We use logback as a logging system in our own system.
Our system uses log4j as the logging system used in A.JarMagneA.jar.
Our system also uses the log system used in B. jar, which is called slf4j-simple.
In this way, our system has to support and maintain the three logging frameworks of logback, log4j and slf4j-simple at the same time, which is very inconvenient.
Slf4j is only a logging standard, not a concrete implementation of the logging system. It's important to understand this sentence. Slf4j does only two things:
Provide log interface
Provides a method to get specific log objects
4. If you use Android directly, the log will conflict
5. Test
Idea automatically adds private static final Logger log1 = LoggerFactory.getLogger (EmpController.class) after logd+enter (enter).
Where d stands for debug,i, info,w for warn.
@ Controller@CrossOrigin@ResponseBodypublic class EmpController {private static final Logger log= LoggerFactory.getLogger (EmpController.class); @ Autowired private EmpService empService; @ RequestMapping ("/ emp/queryAll") public List queryall () {List emps = empService.showEmp (); for (Emp emp: emps) {log.debug ("); log.info ("); log.warn (") } return emps;}
6. Use the space-occupying method to write the log instead of the + sign stitching
{} represents occupancy, and one {} represents occupancy.
@ Controller@CrossOrigin@ResponseBodypublic class EmpController {private static final Logger log= LoggerFactory.getLogger (EmpController.class); @ Autowired private EmpService empService; @ RequestMapping ("/ emp/queryAll") public List queryall () {List emps = empService.showEmp (); for (Emp emp: emps) {log.info ("employee information {} {}", emp, "this is a placeholder");} return emps;}
The above is all the content of the article "sample Analysis of Log Log Integration in SpringBoot". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.