almost 3 years ago

在某一次問卷中看到了不少人選擇此套件作為log工具,引起了我的好奇,所以趁這次拿來練練看
可以很好的整合slf4j,也有自動完成載入設定的機制,用起來比log4j還簡單,更重要的是又小又快

廢話不多說,先添加套件,slf4j已經被設定為相依性套件,所以你不用再指定

build.gradle
dependencies {
    compile 'ch.qos.logback:logback-classic:1.1.2'
}

在resources資料夾新增xml

logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <contextName>Logback Demo</contextName>
    <property name="LOG_HOME" value="D:/logs" />
    <property name="CHARSET" value="UTF-8" />
    <property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" />
    <!-- 控制台輸出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoder默認配置為PartternLayoutEncoder    -->
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${PATTERN}</pattern>
            <charset>${CHARSET}</charset>
        </encoder>
    </appender>
    <!-- 持續輸出的日誌檔案 -->
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_HOME}/myLog.log</file>
        <encoder>
            <pattern>${PATTERN}</pattern>
            <charset>${CHARSET}</charset>
        </encoder>
    </appender>
    <!-- 按照每天生成日誌文件 -->
    <appender name="ROLLINGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日誌文件輸出的文件名-->
            <FileNamePattern>${LOG_HOME}/root-%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--日誌文件保留天數-->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${PATTERN}</pattern>
            <charset>${CHARSET}</charset>
        </encoder>
        <!--日誌文件最大的大小-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
    <!-- 系統用每日生成日誌文件 -->
    <appender name="SYSTEMROLLINGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日誌文件輸出的文件名-->
            <FileNamePattern>${LOG_HOME}/system-%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--日誌文件保留天數-->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${PATTERN}</pattern>
            <charset>${CHARSET}</charset>
        </encoder>
        <!--日誌文件最大的大小-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
    <!-- 超出固定大小後壓縮檔案的日誌 -->
    <appender name="DFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/debug.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_HOME}/debug.%i.log.zip</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>3</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>1MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>
    <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" additivity="false" />
    <logger name="com.piedra.logback" level="WARN" additivity="false">
        <appender-ref ref="FILE" />
    </logger>
    <logger name="system" level="INFO" additivity="false">
        <appender-ref ref="SYSTEMROLLINGFILE" />
    </logger>
    <root level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="ROLLINGFILE" />
    </root>
</configuration>

如果沒有指定路徑檔案會跑到你的IDE目錄下,我的是會跑到D:\IDE\eclipse\system.log

如果試不出來的話可以在程式內添加

LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
StatusPrinter.print(lc);

你就可以知道大概是哪裡出問題了

12:49:22,076 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
12:49:22,078 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
12:49:22,093 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
12:49:22,093 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Setting up default configuration.
程式中使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private Logger logger = LoggerFactory.getLogger("system");
private Logger loggerroot = LoggerFactory.getLogger(Job1.class);

收工....

从Log4j迁移到LogBack的理由
Chapter 1: Introduction
logback 常用配置详解
Logback-学习笔记

← scala、spray、IntelliJ環境安裝篇 第一個spray應用 →
 
comments powered by Disqus