https://gitee.com/DanShenGuiZu/learnDemo/tree/master/logback-learn
用来设置某一个包或者具体的某一个类的日志打印级别、以及指定< appender>
有3个属性:
< logger>可以包含零个或多个< appender-ref>元素,标识这个appender将会添加到这个logger
用来指定受此logger约束的某一个包或者具体的某一个类
public class Demo2 {
private static final Logger limeFloger = LoggerFactory.getLogger(Demo2.class);
public static void main(String[] args) {
limeFloger.trace("------trace");
limeFloger.debug("------debug");
limeFloger.info("------info");
limeFloger.warn("------warn");
limeFloger.error("------error");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="contextName" value="limeLog"/>
<timestamp key="bySecond" datePattern="yyyyMMdd'T'HH:mm:ss"/>
<contextName>${contextName} - ${bySecond}</contextName>
<!-- 表示打印到控制台 -->
<appender name="limeFlogger" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder 默认配置为PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root>
<appender-ref ref="limeFlogger"/>
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="contextName" value="limeLog"/>
<timestamp key="bySecond" datePattern="yyyyMMdd'T'HH:mm:ss"/>
<contextName>${contextName} - ${bySecond}</contextName>
<!-- 表示打印到控制台 -->
<appender name="limeFlogger" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder 默认配置为PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!--
1. 将控制fei.zhou.logbacklearn.business.demo包下的所有类的日志的打印
2. 没用设置打印级别,所以继承他的上级<root>的日志级别"info";
3. 没有设置addtivity,默认为true,将此loger的打印信息向上级传递
4. 没有设置appender,此loger本身不打印任何信息。
-->
<logger name="fei.zhou.logbacklearn.business.demo"/>
<!-- 将root的打印级别设置为"info",指定了名字为"limeLogback"的appender。-->
<root level="info">
<!--使用哪个输出配置-->
<appender-ref ref="limeFlogger"/>
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="contextName" value="limeLog"/>
<timestamp key="bySecond" datePattern="yyyyMMdd'T'HH:mm:ss"/>
<contextName>${contextName} - ${bySecond}</contextName>
<!-- 表示打印到控制台 -->
<appender name="limeFlogger" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder 默认配置为PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- 将控制fei.zhou.logbacklearn.business.demo包下的所有类的日志的打印 -->
<logger name="fei.zhou.logbacklearn.business.demo"/>
<!-- 将控制fei.zhou.logbacklearn.business.demo.Demo2 类的日志的打印 -->
<logger name="fei.zhou.logbacklearn.business.demo.Demo2" level="info" additivity="false">
<appender-ref ref="limeFlogger"/>
</logger>
<root level="error">
<appender-ref ref="limeFlogger"/>
</root>
</configuration>
如果将< logger name=“fei.zhou.logbacklearn.business.demo.Demo2” level=“info” additivity=“false”> 的additivity 修改为true,日志将会打印两次,因为打印信息向上级传递了打印信息