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

缘起

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

配置

  1. 先拷贝一份infoAppender配置过来改个名
  <!-- 监控日志 -->
    <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配置
    <logger name="monitorLogger">
        <appender-ref ref="monitorAppender" />
    </logger>
  1. 代码里面获取一下这个monitorLogger,然后输出就好了
//获取
private static Logger monitorLogger = LoggerFactory.getLogger("monitorLogger");
//使用
monitorLogger.info(stopWatch.prettyPrint(TimeUnit.MILLISECONDS));

完整配置参考

<?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>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值