logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <!--指定日志存储目录-->
    <property name="LOG_HOME" value="/data/logs"/>

    <springProperty name="APP" scope="context" source="spring.application.name"/>
    <springProperty name="APP_NAME" scope="context" source="spring.application.name" />
    <springProperty name="ServerIP" scope="context" source="spring.cloud.client.ip-address" defaultValue="0.0.0.0"/>
    <springProperty name="ServerPort" scope="context" source="server.port" defaultValue="0000"/>

    <!-- 彩色日志 -->
    <!-- 彩色日志依赖的渲染类 -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />

<!--    <property name="CONSOLE_LOG_PATTERN"-->
<!--              value="[${APP_NAME}:${ServerIP}:${ServerPort}] [%clr(%X{traceId}){yellow},%clr(%X{X-B3-TraceId}){yellow}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%thread]){orange} %clr(%logger){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />-->
    <property name="LOG_PATTERN"
              value="[${APP}:${ServerIP}:${ServerPort}] [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-B3-ParentSpanId:-},%X{X-Span-Export:-}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%thread]){orange} %clr(%logger){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
<!--    %d{yyyy-MM-dd HH:mm:ss.SSS} ${APP} [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-B3-ParentSpanId:-},%X{X-Span-Export:-}] [%thread] %-5level %logger{36} - %msg%n-->
    <!--日志模板:控制台打印日志-->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"   >
        <encoder>
                <pattern>${LOG_PATTERN}</pattern>
<!--            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ${APP_NAME} [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-B3-ParentSpanId:-},%X{X-Span-Export:-}] [%thread] %-5level %logger{36} - %msg%n</pattern>-->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--日志模板:不过滤日志等级都打印到detail.log-->
    <appender name="DETAIL" class="ch.qos.logback.core.rolling.RollingFileAppender" >
        <!--<file>${LOG_HOME}/${APP_NAME}/detail.log</file>-->
        <encoder>
<!--            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ${APP_NAME} [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-B3-ParentSpanId:-},%X{X-Span-Export:-}] [%thread] %-5level %logger{36} - %msg%n</pattern>-->
            <pattern>${LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${APP_NAME}/%d{yyyy-MM-dd}/detail.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>
    <!--日志模板:warn日志等级都打印到warn.log-->
    <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--<File>${LOG_HOME}/${APP_NAME}/warn.log</File>-->
        <encoder>
<!--            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ${APP_NAME} [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-B3-ParentSpanId:-},%X{X-Span-Export:-}] [%thread] %-5level %logger{36} - %msg%n</pattern>-->
            <pattern>${LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${APP_NAME}/%d{yyyy-MM-dd}/warn.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <!--日志模板:error日志等级都打印到error.log-->
    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender" >
        <!--<File>${LOG_HOME}/${APP_NAME}/error.log</File>-->
        <encoder>
<!--            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ${APP_NAME} [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-B3-ParentSpanId:-},%X{X-Span-Export:-}] [%thread] %-5level %logger{36} - %msg%n</pattern>-->
            <pattern>${LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${APP_NAME}/%d{yyyy-MM-dd}/error.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!--com.haiercash包所有日志会被打印-->
    <logger name="com.haiercash" level="DEBUG" >
        <appender-ref ref="DETAIL" />
    </logger>

    <!--全包错误大于等于WARN等级,会被打印,过滤布必要打印的日志-->
    <!--开发环境:打印控制台-->
<!--    <springProfile name="beta">-->
<!--        <logger name="com.haiercash.temp" level="debug"/>-->
<!--    </springProfile>-->
    <logger name="org.apache.kafka.clients.producer.ProducerConfig" level="off" />
    <logger name="org.apache.kafka.clients.consumer.ConsumerConfig" level="off" />

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="DETAIL" />
        <appender-ref ref="ERROR"/>
        <appender-ref ref="WARN"/>
    </root>
</configuration>

 

上一篇:logback-access 的使用


下一篇:lombok+Logback统一异常和日志处理