完整的日志文件

日志拆分的原因:

1、抽取公共部分统一管理

2、统一日志格式

3、方便维护

 

1、新建base-logback.xml

 


<!--定义公共配置-->
<included>
    <!--日志公共配置-->
    <!--日志格式-->
    <property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %X{TRACE_ID} [%thread] %-5level %logger[%L] - %m%n"/>
    <!--日志编码-->
    <property name="CHARSET" value="utf-8"/>
    <!--单个日志文件大小-->
    <property name="MAX_FILE_SIZE" value="10Kb"/>
    <!--日志过期时间-->
    <property name="MAX_HISTORY" value="1"/>
    <!--日志根路径-->
    <property name="BASE_LOG_PATH" value="/data/logs"/>

    <!--控制台打印日志-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${PATTERN}</pattern>
            <charset>${CHARSET}</charset>
        </encoder>
    </appender>


    <appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--当前日志文件名-->
        <File>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-info.log</File>
        <!--日志级别过滤-->
        <filter class="com.luna.log.filter.LogbackInfoAndDebugFilter"></filter>
        <!--日志滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志归档文件名-->
            <fileNamePattern>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-info.log.%d{yyyy-MM-dd}.log.%i.gz
            </fileNamePattern>
            <!--按天滚动-->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!--单个日志文件最大,也会滚动-->
                <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--日志存活时间,大于这个时间的日志都将会删除-->
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
        <encoder>
            <!--日志格式-->
            <pattern>${PATTERN}</pattern>
            <!--日志编码-->
            <charset>${CHARSET}</charset>
        </encoder>
    </appender>


    <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-error.log</File>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-error.log.%d{yyyy-MM-dd}.log.%i.gz
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${PATTERN}</pattern>
            <charset>${CHARSET}</charset>
        </encoder>
    </appender>


    <!--开启logback异步记录日志-->
    <appender name="async-console" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="console"/>
        <includeCallerData>true</includeCallerData>
    </appender>

    <appender name="async-fileAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="fileAppender"/>
        <includeCallerData>true</includeCallerData>
    </appender>

    <appender name="async-errorAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="errorAppender"/>
        <includeCallerData>true</includeCallerData>
    </appender>


    <!--整个项目打印INFO 以上级别-->
    <logger name="com.luna" level="INFO">
        <appender-ref ref="async-fileAppender"/>
        <appender-ref ref="async-errorAppender"/>
    </logger>

    <!--记录druid-sql的记录-->
    <logger name="druid.sql.Statement" level="debug" additivity="false">
        <appender-ref ref="async-console"/>
        <appender-ref ref="async-fileAppender"/>
    </logger>
</included>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.

 

2、新建logback-spring.xml引用base-logback.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!--日志服务名-->
    <property scope="local" name="SERVICE_NAME" value="demo"/>

    <!--引用公共日志-->
    <include resource="base-logback.xml"/>

    <!--全局打印INFO 级别日志-->
    <root level="INFO">
        <appender-ref ref="async-console"/>
    </root>

</configuration>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

 

因为我的项目是springboot构建,所以命名logback-spring.xml,这种命名可以使用spring标签