logback-spring.xml 中根据不同的业务表示,分类打印到不同的文件夹、时区动态设置

logback-spring.xml 中根据不同的业务表示,分类打印到不同的文件夹、时区动态设置

logback-spring.xml 完整配置


<configuration debug="false" scan="true" scanPeriod="60 seconds">
    <springProperty scop="context" name="spring.application.name" source="spring.application.name" defaultValue=""/>
    <springProperty scop="context" name="timeZone" source="timezone.defaultTimeZone" defaultValue=""/>
    <springProperty scop="context" name="spring.application.cloud.discovery.namespace"
                    source="spring.application.cloud.discovery.namespace" defaultValue="dev"/>
    <property name="log.path" value="logs"/>

    
    <property name="container.id" value="${VERSION:-0}"/>
    
    <property name="CONSOLE_LOG_PATTERN"
              value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS, ${timeZone}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} [%5L] %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
    
    <property name="log.pattern"
              value="%d{yyyy-MM-dd HH:mm:ss.SSS, ${timeZone}} [%21.21t] %-5level [%-40.40logger{36}] [%line] - %msg%n"/>
    
    <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"/>

    
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}pattern>
        encoder>
    appender>

    <appender name="INFO" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>appCodekey>
            <defaultValue>defaultdefaultValue>
        discriminator>
        <sift>
            <appender name="FILE-${appCode}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                    <fileNamePattern>
                        ${log.path}/${spring.application.name}/${appCode}/${spring.application.name}-${container.id}.%d{yyyy-MM-dd,${timeZone}}.%i.log
                    fileNamePattern>
                    <maxFileSize>50MBmaxFileSize>
                    <maxHistory>30maxHistory>
                rollingPolicy>
                <encoder>
                    <pattern>${log.pattern}pattern>
                encoder>
                <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                    <level>INFOlevel>
                filter>
            appender>
        sift>
    appender>

    <appender name="ERROR" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>appCodekey>
            <defaultValue>defaultdefaultValue>
        discriminator>
        <sift>
            <appender name="FILE-${appCode}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                    <fileNamePattern>
                        ${log.path}/${spring.application.name}/${appCode}/error/${spring.application.name}-error-${container.id}.%d{yyyy-MM-dd,${timeZone}}.%i.log
                    fileNamePattern>
                    <maxFileSize>50MBmaxFileSize>
                    <maxHistory>30maxHistory>
                rollingPolicy>
                <encoder>
                    <pattern>${log.pattern}pattern>
                encoder>
                <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                    <level>ERRORlevel>
                filter>
            appender>
        sift>
    appender>


    <appender name="android-appender" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>appCodekey>
            <defaultValue>defaultdefaultValue>
        discriminator>
        <sift>
            <appender name="FILE-${appCode}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                    <fileNamePattern>
                        ${log.path}/${spring.application.name}/${appCode}/android/${spring.application.name}-${container.id}.%d{yyyy-MM-dd,${timeZone}}.%i.log
                    fileNamePattern>
                    <maxFileSize>100MBmaxFileSize>
                    <maxHistory>30maxHistory>
                rollingPolicy>
                <encoder>
                    <pattern>${log.pattern}pattern>
                encoder>
            appender>
        sift>
    appender>

    <logger name="android" additivity="false">
        <appender-ref ref="android-appender"/>
    logger>

    <root level="info">
        <appender-ref ref="INFO"/>
        <appender-ref ref="ERROR"/>
        <appender-ref ref="console"/>
    root>

configuration>

根据appCode存储日志

我的是根据不同的appCode,打印不同的文件夹中,如果没有拿到appCode,就打印到default中

在拦截器中,网mdc中put对应的appCode,在xml中使用${appCode}获取即可

时区动态设置

application.yml

timezone:
  defaultTimeZone: 时区

定义属性关键代码:

<springProperty scop="context" name="timeZone" source="timezone.defaultTimeZone" defaultValue=""/>

使用取值:

${log.path}/${spring.application.name}/${appCode}/${spring.application.name}-${container.id}.%d{yyyy-MM-dd,${timeZone}}.%i.log

你可能感兴趣的:(logback,xml,spring,boot)