美文网首页
logback的appender和logger

logback的appender和logger

作者: liuyangcc | 来源:发表于2017-11-28 14:58 被阅读0次

实际打印log的地方

logger.trace("-- this is a trace info");
logger.debug("-- this is a debug info");
logger.info("-- this is an info info");
logger.warn("-- this is a warn info");
logger.error("-- this is an error info");

Situation 1

只有一个root控制appender的日志等级
logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_HOME" value="log/" />
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/base-springboot-web-logback.log.%d{yyyy-MM-dd}</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>50MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

实际的log如下

017-11-11 02:08:25.211 [main] WARN  c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is a warn info
2017-11-11 02:08:25.211 [main] INFO  c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is an info info
2017-11-11 02:08:25.211 [main] ERROR c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is an error info

Situation 2

在logback-spring.xml中添加一个logger,level为debug,其实可以不设置level,默认level即为debug。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_HOME" value="log/" />
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/base-springboot-web-logback.log.%d{yyyy-MM-dd}</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>50MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <logger name="com.lerr.basespringbootweb"/>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

输出如下,会看到debug的消息也打印到日志了

2017-11-11 21:30:44.174 [main] DEBUG c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is a debug info
2017-11-11 21:30:44.174 [main] INFO  c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is an info info
2017-11-11 21:30:44.174 [main] WARN  c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is a warn info
2017-11-11 21:30:44.174 [main] ERROR c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is an error info

Situation 3

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_HOME" value="log/" />
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/base-springboot-web-logback.log.%d{yyyy-MM-dd}</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>50MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <logger name="com.lerr.basespringbootweb" level="DEBUG">
        <appender-ref ref="STDOUT" />
    </logger>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

output

2017-11-11 21:46:31.081 [main] DEBUG c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is a debug info
2017-11-11 21:46:31.081 [main] DEBUG c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is a debug info
2017-11-11 21:46:31.083 [main] INFO  c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is an info info
2017-11-11 21:46:31.083 [main] INFO  c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is an info info
2017-11-11 21:46:31.083 [main] WARN  c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is a warn info
2017-11-11 21:46:31.083 [main] WARN  c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is a warn info
2017-11-11 21:46:31.083 [main] ERROR c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is an error info
2017-11-11 21:46:31.083 [main] ERROR c.l.basespringbootweb.BaseSpringbootWebApplication - -- this is an error info

相关文章

网友评论

      本文标题:logback的appender和logger

      本文链接:https://www.haomeiwen.com/subject/uaxwmxtx.html