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
相关推荐
NiNg_1_2342 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
种树人202408192 小时前
如何在 Spring Boot 中启用定时任务
spring boot
苹果醋35 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
Wx-bishekaifayuan5 小时前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
customer085 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
Yaml46 小时前
智能化健身房管理:Spring Boot与Vue的创新解决方案
前端·spring boot·后端·mysql·vue·健身房管理
LuckyLay6 小时前
Spring学习笔记_27——@EnableLoadTimeWeaving
java·spring boot·spring
佳佳_7 小时前
Spring Boot 应用启动时打印配置类信息
spring boot·后端
程序媛小果8 小时前
基于java+SpringBoot+Vue的宠物咖啡馆平台设计与实现
java·vue.js·spring boot
狂放不羁霸10 小时前
idea | 搭建 SpringBoot 项目之配置 Maven
spring boot·maven·intellij-idea