首页 > 学院 > 开发设计 > 正文

Logback 入门详解

2019-11-10 19:52:57
字体:
来源:转载
供稿:网友

    Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。         --转自百度百科

     Logback官方网址为:https://logback.qos.ch/ 

     由于Logback比log4j和SLF4J拥有众多优点,如性能(据说有时达到10倍以上),并且支持自动加载配置文件,自动删除旧的日志文件,以及同一个logback配置文件同时适应开发,测试,生产等。因此Logback官方强烈建议开发人员从log4j转到使用Logback。

    一、实战

      1. logback的maven依赖:   

        <dependency>            <groupId>ch.qos.logback</groupId>            <artifactId>logback-core</artifactId>            <version>1.1.7</version>        </dependency>        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->        <dependency>            <groupId>ch.qos.logback</groupId>            <artifactId>logback-classic</artifactId>            <version>1.1.7</version>        </dependency>        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-access -->        <dependency>            <groupId>ch.qos.logback</groupId>            <artifactId>logback-access</artifactId>            <version>1.1.7</version>        </dependency>

     2. logback.xml文件配置:

<?xml version="1.0" encoding="UTF-8"?><configuration debug="true" scan="true" scanPeriod="30 seconds">    <!--控制台输出日志信息-->    <appender name="CONSOLE-LOG" class="ch.qos.logback.core.ConsoleAppender">        <!-- encoders are  by default assigned the type             ch.qos.logback.classic.encoder.PatternLayoutEncoder -->        <encoder>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%level] - %m%n</pattern>        </encoder>    </appender>    <!-- 输出error级别的日志到error.log文件中 -->    <appender name="ERROR-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">        <file>logs/error.log</file>        <encoder>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n</pattern>        </encoder>        <filter class="ch.qos.logback.classic.filter.LevelFilter">            <level>ERROR</level>            <onMatch>ACCEPT</onMatch>            <onMismatch>DENY</onMismatch>        </filter>        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <!-- daily rollover -->            <fileNamePattern>error.%d{yyyy-MM-dd}.log.zip</fileNamePattern>            <!-- keep 30 days' worth of history -->            <maxHistory>30</maxHistory>        </rollingPolicy>    </appender>    <!-- 输出info级别的日志到error.log文件中 -->    <appender name="INFO-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">        <file>logs/info.log</file>        <encoder>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n</pattern>        </encoder>        <filter class="ch.qos.logback.classic.filter.LevelFilter">            <level>INFO</level>            <onMatch>ACCEPT</onMatch>            <onMismatch>DENY</onMismatch>        </filter>        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">            <fileNamePattern>info.%i.log</fileNamePattern>            <minIndex>1</minIndex>            <maxIndex>3</maxIndex>        </rollingPolicy>        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">            <maxFileSize>5MB</maxFileSize>        </triggeringPolicy>    </appender>    <appender name="DEBUG-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">        <file>logs/debug.log</file>        <encoder>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n</pattern>        </encoder>        <filter class="ch.qos.logback.classic.filter.LevelFilter">            <level>INFO</level>            <onMatch>ACCEPT</onMatch>            <onMismatch>DENY</onMismatch>        </filter>        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">            <fileNamePattern>debug.%i.log</fileNamePattern>            <minIndex>1</minIndex>            <maxIndex>3</maxIndex>        </rollingPolicy>        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">            <maxFileSize>5MB</maxFileSize>        </triggeringPolicy>    </appender>    <root level="debug">        <appender-ref ref="CONSOLE-LOG" />        <appender-ref ref="ERROR-LOG" />        <appender-ref ref="INFO-LOG" />        <appender-ref ref="DEBUG-LOG" />    </root></configuration>      logback的项目目录结构:

                      

 %20 3.LogBackTest.java %20 %20 %20 

import%20org.junit.Test;import%20org.slf4j.Logger;import%20org.slf4j.LoggerFactory;public%20class%20LogBackTest%20{%20%20%20%20PRivate%20static%20Logger%20Log%20=%20LoggerFactory.getLogger(LogBackTest.class);%20%20%20%20@Test%20%20%20%20public%20void%20test()%20{%20%20%20%20%20%20%20%20Log.debug("debug%20log");%20%20%20%20%20%20%20%20Log.info("info%20log");%20%20%20%20%20%20%20%20Log.error("error%20log");%20%20%20%20}}

 %20 %20 %20 4.%20结果输出:

 %20 %20 %20 %204.1%20console:%20 %20 %20 %20 

2017-02-08%2014:41:10%20[DEBUG]%20-%20debug%20log2017-02-08%2014:41:10%20[INFO]%20-%20info%20log2017-02-08%2014:41:10%20[ERROR]%20-%20error%20log

 %20 %20 %20 %204.2%20日志文件输出:%20 

 %20 %20 %20 %20       

     debug.log的内容:      

2017-02-08 14:41:10 [LogBackTest:13] - info log     error.log的内容:     

2017-02-08 14:41:10 [LogBackTest:14] - error log     info.log的内容:

2017-02-08 14:41:10 [LogBackTest:13] - info log

看,简单吧!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表