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

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

logback-spring.xml 完整配置

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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}"/>
    <!--    %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %file:%line - %msg%n-->
    <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"/>

    <!-- Console log output -->
    <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>appCode</key>
            <defaultValue>default</defaultValue>
        </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>50MB</maxFileSize>
                    <maxHistory>30</maxHistory>
                </rollingPolicy>
                <encoder>
                    <pattern>${log.pattern}</pattern>
                </encoder>
                <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                    <level>INFO</level>
                </filter>
            </appender>
        </sift>
    </appender>

    <appender name="ERROR" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>appCode</key>
            <defaultValue>default</defaultValue>
        </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>50MB</maxFileSize>
                    <maxHistory>30</maxHistory>
                </rollingPolicy>
                <encoder>
                    <pattern>${log.pattern}</pattern>
                </encoder>
                <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                    <level>ERROR</level>
                </filter>
            </appender>
        </sift>
    </appender>


    <appender name="android-appender" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>appCode</key>
            <defaultValue>default</defaultValue>
        </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>100MB</maxFileSize>
                    <maxHistory>30</maxHistory>
                </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

yaml 复制代码
timezone:
  defaultTimeZone: 时区

定义属性关键代码:

xml 复制代码
<springProperty scop="context" name="timeZone" source="timezone.defaultTimeZone" defaultValue=""/>

使用取值:

xml 复制代码
${log.path}/${spring.application.name}/${appCode}/${spring.application.name}-${container.id}.%d{yyyy-MM-dd,${timeZone}}.%i.log
相关推荐
弗拉唐5 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
2401_857610036 小时前
SpringBoot社团管理:安全与维护
spring boot·后端·安全
凌冰_7 小时前
IDEA2023 SpringBoot整合MyBatis(三)
spring boot·后端·mybatis
天天进步20157 小时前
Vue+Springboot用Websocket实现协同编辑
vue.js·spring boot·websocket
乌啼霜满天2498 小时前
Spring 与 Spring MVC 与 Spring Boot三者之间的区别与联系
java·spring boot·spring·mvc
tangliang_cn8 小时前
java入门 自定义springboot starter
java·开发语言·spring boot
找藉口是失败者的习惯8 小时前
从传统到未来:Android XML布局 与 Jetpack Compose的全面对比
android·xml
Grey_fantasy8 小时前
高级编程之结构化代码
java·spring boot·spring cloud
一二小选手9 小时前
【MyBatis】全局配置文件—mybatis.xml 创建xml模板
xml·java·mybatis