当前位置:网站首页>Logback logger and root

Logback logger and root

2022-04-23 14:12:00 All the names I thought of were used

One 、root and logger

<logger> Used to set the log printing level of a package or a specific class 、 And the designation appender. It can contain zero or more <appender-ref> Elements , Identify this appender Will be added to this logger.
<root> It's also <logger> Elements , But it's a root logger, only one level attribute , Because of its name Namely ROOT

Next, let's talk about logger and root Use

scene

logback To configure

   <logger name="com.codexie.controller.demo1" level="debug">
        <appender-ref ref="Stdout" />
        <appender-ref ref="RollingFile" />
    </logger>
    <logger name="com.codexie.controller.demo2" level="warn"></logger>
    <!--  Log output level  -->
    <root level="info">
        <appender-ref ref="Stdout" />   
        <appender-ref ref="RollingFile" />   
    </root> 

We can see demo1 Of level Set to debug,demo2 Of level by warn, and root Of level by info
controller

@Controller
public class demo1 {
    private static final Logger logger = LoggerFactory.getLogger(demo1.class);

    @RequestMapping("demo1")
    @ResponseBody
    public String logBack(){
        logger.debug("demo1:logback debug");
        logger.info("demo1:logback info");
        return "ok";
    }
}


@Controller
public class demo2 {
    private static final Logger logger = LoggerFactory.getLogger(demo2.class);

    @RequestMapping("demo2")
    @ResponseBody
    public String logBack(){
        logger.info("demo2:logback run");
        logger.debug("demo2:logback info");
        logger.warn("demo2:logback warn");
        return "ok";
    }
}

visit demo1
 Insert picture description here
You can see that the same message has been printed twice , because demo1 Of level yes debug, therefore debug and info All levels of information can · Print , Again because logger and root There are appender, So the same message is printed twice .
visit demo2
 Insert picture description here
visit demo2 Only one message was printed , because demo2 Of level by warn, So it won't print debug and info Level information , Only print warn Level information , Again because demo2 Of logger No configuration appender, So use root Of appender Print once .

Scenario two

 <logger name="com.codexie.controller.demo1" level="debug"> </logger>
  <logger name="com.codexie.controller.demo2" level="warn"></logger>
    <!--  Log output level  -->
    <root level="info">
        <appender-ref ref="Stdout" />   
        <appender-ref ref="RollingFile" />   
    </root> 

We will demo1 Of appender And cancelled , Verify to see if demo1 The log information of is only printed once
 Insert picture description here
Verify success , The conclusion is correct

版权声明
本文为[All the names I thought of were used]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231404419674.html