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>
相关推荐
许苑向上1 小时前
MVCC底层原理实现
java·数据库·mvcc原理
组合缺一1 小时前
Solon Cloud Gateway 开发:熟悉 ExContext 及相关接口
java·后端·gateway·solon
一只淡水鱼662 小时前
【spring】集成JWT实现登录验证
java·spring·jwt
忘忧人生2 小时前
docker 部署 java 项目详解
java·docker·容器
null or notnull3 小时前
idea对jar包内容进行反编译
java·ide·intellij-idea·jar
言午coding4 小时前
【性能优化专题系列】利用CompletableFuture优化多接口调用场景下的性能
java·性能优化
幸好我会魔法4 小时前
人格分裂(交互问答)-小白想懂Elasticsearch
大数据·spring boot·后端·elasticsearch·搜索引擎·全文检索
危险、4 小时前
Spring Boot 无缝集成SpringAI的函数调用模块
人工智能·spring boot·函数调用·springai
缘友一世4 小时前
JAVA设计模式:依赖倒转原则(DIP)在Spring框架中的实践体现
java·spring·依赖倒置原则
何中应5 小时前
从管道符到Java编程
java·spring boot·后端