Spring Boot使用Logback将某些日志输出到单独的文件

缘起

重构了某个接口,想监控下新接口各个处理过程的时间,从而评估和监控日常性能。监控使用Spring自带的StopWatch,日志使用Logback

配置

  1. 先拷贝一份infoAppender配置过来改个名
xml 复制代码
  <!-- 监控日志 -->
    <appender name="monitorAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.dir}/logs/${project_name}_monitor.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.dir}/logs/${project_name}_monitor_%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>${MONITOR_PATTERN}</pattern>
        </encoder>
    </appender>
  1. 添加单独的logger配置
xml 复制代码
    <logger name="monitorLogger">
        <appender-ref ref="monitorAppender" />
    </logger>
  1. 代码里面获取一下这个monitorLogger,然后输出就好了
java 复制代码
//获取
private static Logger monitorLogger = LoggerFactory.getLogger("monitorLogger");
//使用
monitorLogger.info(stopWatch.prettyPrint(TimeUnit.MILLISECONDS));

完整配置参考

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="false">
    <property name="project_name" value="xxx"/>
    <property name="log.dir" value="/log/xxx"/> 
    <property name="MONITOR_PATTERN"
              value="[%d{yyyy-MM-dd HH:mm:ss.SSS}]-[%X{EagleEye-TraceID}][%thread]-[%-5level]-[%logger{50}:%L] - %msg%n"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.out</target>
        <encoder charset="UTF-8">
            <pattern>${MONITOR_PATTERN}</pattern>
        </encoder>
    </appender>

    <appender name="infoAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.dir}/logs/${project_name}_info.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.dir}/logs/${project_name}_info_%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>${MONITOR_PATTERN}</pattern>
        </encoder>
    </appender>

    <!-- 监控日志 -->
    <appender name="monitorAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.dir}/logs/${project_name}_monitor.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.dir}/logs/${project_name}_monitor_%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>${MONITOR_PATTERN}</pattern>
        </encoder>
    </appender>

    <appender name="errorAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.dir}/logs/${project_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>${log.dir}/logs/${project_name}_error_%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>${MONITOR_PATTERN}</pattern>
        </encoder>
    </appender>

    <logger name="monitorLogger">
        <appender-ref ref="monitorAppender" />
    </logger>
    <root>
        <level value="INFO"/>
<!--        <appender-ref ref="STDOUT"/>-->
        <appender-ref ref="infoAppender"/>
        <appender-ref ref="errorAppender"/>
    </root>

</configuration>
相关推荐
向阳121818 分钟前
mybatis 缓存
java·缓存·mybatis
上等猿24 分钟前
函数式编程&Lambda表达式
java
蓝染-惣右介42 分钟前
【23种设计模式·全精解析 | 行为型模式篇】11种行为型模式的结构概述、案例实现、优缺点、扩展对比、使用场景、源码解析
java·设计模式
等一场春雨1 小时前
springboot 3 websocket react 系统提示,选手实时数据更新监控
spring boot·websocket·react.js
秋恬意1 小时前
IBatis和MyBatis在细节上的不同有哪些
java·mybatis
荆州克莱2 小时前
Golang的性能监控指标
spring boot·spring·spring cloud·css3·技术
齐 飞2 小时前
BeanFactory和FactoryBean
java·sprint
大霞上仙2 小时前
lxml 解析xml\html
java·服务器·网络
Xiaoweidumpb2 小时前
tomcat temp临时文件不清空,占用硬盘,jdk字体内存泄漏
java·tomcat