先说一下场景,app接入网宿,通过打印请求响应日志,看看是不是有效果,哈哈哈哈哈哈。
于是乎,加了请求日志,加了响应日志,请求和响应都加了时间戳。因为app处于一个起步阶段,即便是生产环境其实也有很多日志。以前的老大设计了单独的日志,比如支付的单独一套日志,响应也会有单独的一套日志。还是很感谢以前的老大的设计。我也试试吧。
spring boot + slf4j
打印请求响应日志,加了个切面,具体的代码就不说了,往上一堆一堆的。只介绍日志如何单独打印。
首先看slf4j配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
<include resource="org/springframework/boot/logging/logback/base.xml" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level> <!--过滤掉error的级别-->
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<file>/server/tomcat8-server/logs/maker.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/server/tomcat8-server/logs/maker.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] %logger:%line : %msg%n</pattern>
</encoder>
</appender>
<logger name="FILELOG" level="info">
<appender-ref ref="FILE"/>
</logger>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 只写入error级别的日志 -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<file>/server/tomcat8-server/logs/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/server/tomcat8-server/logs/error.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] %logger:%line : %msg%n</pattern>
</encoder>
</appender>
<logger name="ERRORLOG" level="error">
<appender-ref ref="fileErrorLog"/>
</logger>
<appender name="reqresplog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/server/tomcat8-server/logs/reqresp.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/server/tomcat8-server/logs/reqresp.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] %logger:%line : %msg%n</pattern>
</encoder>
</appender>
<logger name="reqresp" level="info">
<appender-ref ref="reqresplog" />
</logger>
<root level="INFO">
<!--<appender-ref ref="CONSOLE"/>-->
<appender-ref ref="FILE"/>
<!--<appender-ref ref="dailyRollingFileAppender"/>-->
<!--<appender-ref ref="Sentry"/>-->
<!-- <appender-ref ref="fileErrorLog"/>-->
</root>
然后看使用
Logger logger = LoggerFactory.getLogger("reqresp");
log.info("api-request,{}", reqrespJson.toJSONString());
注:应该是要把之前的@Slf4j注掉
servlet+log4j
先看log4j的配置
log4j.rootLogger= debug,stand, NormalLog, ErrorLog
#log4j.rootLogger= fatal
#control log level by package or class
log4j.logger.org.apache.mina=error
log4j.logger.com.mchange=fatal
log4j.logger.org.apache.catalina.startup=error
#NormalLog
log4j.appender.NormalLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.NormalLog.File=/server/tomcat8-api/logs/maker.log
log4j.appender.NormalLog.DatePattern='.'yyyyMMdd
log4j.appender.NormalLog.Threshold=info
log4j.appender.NormalLog.layout=org.apache.log4j.PatternLayout
log4j.appender.NormalLog.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %5p [%t] (%F:%L) - %m%n
#ErrorLog, only log warning/error/fatal
log4j.appender.ErrorLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ErrorLog.File=/server/tomcat8-api/logs/error.log
log4j.appender.ErrorLog.Threshold=error
log4j.appender.ErrorLog.DatePattern='.'yyyyMMdd
log4j.appender.ErrorLog.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorLog.layout.ConversionPattern=%d{MM-dd HH\:mm\:ss} %5p [%t] (%F\:%L) - %m%n
#log4j.logger.com.live.filter.GlobalFilter=DEBUG, reqrespparam
#log4j.logger.com.live.api.utils.Tools=DEBUG, reqrespparam #com.live.filter.GlobalFilter,com.live.api.utils.Tools是要单独输出的类名
#log4j.appender.reqrespparam=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.reqrespparam.Threshold=info
#log4j.appender.reqrespparam.ImmediateFlush=true
#log4j.appender.reqrespparam.Append=true
#log4j.appender.reqrespparam.File=/server/tomcat8-api/logs/reqresp.log
#log4j.appender.reqrespparam..DatePattern='.'yyyyMMdd
#log4j.appender.reqrespparam.layout=org.apache.log4j.PatternLayout
#log4j.appender.reqrespparam.layout.ConversionPattern=%d{MM-dd HH\:mm\:ss} %5p [%t] (%F\:%L) - %m%n
再看使用