logback經常使用配置

1實現.控制檯輸出 mysql

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <target>System.out</target>
    <encoder>
        <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}-%msg%n </Pattern>
        <charset>utf-8</charset>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>error</level>
        <onMatch>DENY</onMatch>
        <onMismatch>ACCEPT</onMismatch>
    </filter>
</appender>
<appender name="STDOUT_ERROR" class="ch.qos.logback.core.ConsoleAppender">
    <target>System.err</target>
    <encoder>
        <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -%msg%n </Pattern>
        <charset>utf-8</charset>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>error</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>
</appender>

2實現:將全部的日誌消息一直追加到指定的文件中sql

<appender name="FILE_APPEND" class="ch.qos.logback.core.FileAppender">
    <file>F:/log/test/logback.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -%msg%n </Pattern>
        <charset>utf-8</charset>
    </encoder>
    <append>true</append>
</appender>

3實現:天天產生一個日誌文件且只保存最近10天的日誌數據庫

<appender name="FILE_LIMITED_ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>F:/log/test/today.log</file>
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>
        <charset>utf-8</charset>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>F:/log/test/%d{yyyy-MM-dd}.log</fileNamePattern>
        <!-- 10 day -->
        <maxHistory>10</maxHistory>
    </rollingPolicy>
</appender>

 4實現.一天產生一個日誌,若是當天日誌超過10M將進行再分割app

<appender name="FILE_DAILY_ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>F:/log/test/today.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n </Pattern>
        <charset>utf-8</charset>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>F:/log/test/%d.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

5實現:全部的日誌消息追加到一個日誌文件中,若是該文件超過100M,就將其打包成zip.(歸檔個數不限)
 注意:*fileNamePattern不能使用%d,
          *若是歸檔文件名爲backup%i.zip,那麼日誌文件的後綴名(log)將丟失
異步

<appender name="FILE_ARCHIVE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
    <file>F:/log/test/logback.log</file> 
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> 
        <fileNamePattern>F:/log/test/backup%i.log.zip</fileNamePattern>     </rollingPolicy> 
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 
        <maxFileSize>100MB</maxFileSize>     </triggeringPolicy> 
    <encoder> 
        <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{36} - %msg%n</pattern>     </encoder>
</appender>

6實現: 【在5實的基礎上添加】只保存最近的3個歸檔文件(.zip)日誌

<appender name="FILE_LIMITED_ARCHIVE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
    <file>F:/log/test/logback.log</file> 
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> 
        <fileNamePattern>F:/log/test/backup%i.log.zip</fileNamePattern> 
        <minIndex>1</minIndex> 
        <maxIndex>3</maxIndex>     </rollingPolicy> 
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 
        <maxFileSize>100MB</maxFileSize>     </triggeringPolicy> 
    <encoder> 
        <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{36} - %msg%n</pattern>     </encoder>
</appender>

7.輸出到數據庫code

<!--日誌異步到數據庫 -->  
    <appender name="DBAppender" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
            <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <driverClass>com.mysql.jdbc.Driver</driverClass>
                <jdbcUrl>jdbc:mysql://xxxx:3306/xy-pay?autoReconnect=true&amp;failOverReadOnly=false&amp;maxReconnects=10&amp;allowMultiQueries=true</jdbcUrl>
                <user>xxx</user>
                <password>xxx</password>
            </dataSource>
        </connectionSource>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>error</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>


建表以下:

BEGIN;  
DROP TABLE IF EXISTS logging_event_property;  
DROP TABLE IF EXISTS logging_event_exception;  
DROP TABLE IF EXISTS logging_event;  
COMMIT;  
  
BEGIN;  
CREATE TABLE logging_event   
  (  
    timestmp         BIGINT NOT NULL,  
    formatted_message  TEXT NOT NULL,  
    logger_name       VARCHAR(254) NOT NULL,  
    level_string      VARCHAR(254) NOT NULL,  
    thread_name       VARCHAR(254),  
    reference_flag    SMALLINT,  
    arg0              VARCHAR(254),  
    arg1              VARCHAR(254),  
    arg2              VARCHAR(254),  
    arg3              VARCHAR(254),  
    caller_filename   VARCHAR(254) NOT NULL,  
    caller_class      VARCHAR(254) NOT NULL,  
    caller_method     VARCHAR(254) NOT NULL,  
    caller_line       CHAR(4) NOT NULL,  
    event_id          BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY  
  );  
COMMIT;  
BEGIN;  
CREATE TABLE logging_event_property  
  (  
    event_id          BIGINT NOT NULL,  
    mapped_key        VARCHAR(254) NOT NULL,  
    mapped_value      TEXT,  
    PRIMARY KEY(event_id, mapped_key),  
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)  
  );  
COMMIT;  
BEGIN;  
CREATE TABLE logging_event_exception  
  (  
    event_id         BIGINT NOT NULL,  
    i                SMALLINT NOT NULL,  
    trace_line       VARCHAR(254) NOT NULL,  
    PRIMARY KEY(event_id, i),  
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)  
  );  
COMMIT;
相關文章
相關標籤/搜索