缘起
重构了某个接口,想监控下新接口各个处理过程的时间,从而评估和监控日常性能。监控使用Spring自带的StopWatch
,日志使用Logback
配置
- 先拷贝一份
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>
- 添加单独的
logger
配置
xml
<logger name="monitorLogger">
<appender-ref ref="monitorAppender" />
</logger>
- 代码里面获取一下这个
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>