一、拦截器
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class LogInterceptor implements HandlerInterceptor {
private final static String LOG_TRACE_ID = "logTraceId";
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
String uuid = UUID.randomUUID().toString();
MDC.put(LOG_TRACE_ID, uuid);
return true;
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
MDC.remove(LOG_TRACE_ID);
}
}
二、注册拦截器
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* WebMvc配置
*
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private LogInterceptor logInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 添加一个拦截器
registry.addInterceptor(logInterceptor).addPathPatterns("/**");
}
}
三、多环境日志配置(开发环境)
(1)application.yml文件中设置
spring:
profiles:
active: dev
(2)application-dev.yml文件中设置
logging:
config: classpath:logback-dev.xml
#logback日志级别 用在
logback:
logLevel: INFO
(3)logback-dev.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 分级别异步文件日志输出配置 -->
<configuration>
<!-- 日志级别 -->
<!--<property name="logLevel" value="DEBUG"></property>-->
<springProperty scope="context" name="logLevel" source="logback.logLevel"/>
<!-- 日志地址 -->
<property name="logPath" value="./logs"></property>
<!-- 最大保存时间 -->
<property name="maxHistory" value="30"/>
<!-- 项目名称 -->
<property name="projectName" value="rent-equipment-towercrane"></property>
<!-- 异步缓冲队列的深度,该值会影响性能.默认值为256 -->
<property name="queueSize" value="512"></property>
<!-- 超过50MB再按日期生成新的文件 -->
<property name="maxFileSize" value="100MB"></property>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> -->
<!-- logback的MDC功能。traceId变量来自于上面LogInterceptor拦截器下的traceId。-->
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%X{traceId}] [%logger] %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="FILE_DEBUG"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- <file>${logPath}/${projectName}/debug/debug_%d.log</file> -->
<!-- SizeAndTimeBasedRollingPolicy:日志按时间和大小滚动 -->
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- %d{yyyy-MM-dd}:每天的日期格式,%i:序号,从0开始 -->
<fileNamePattern>${logPath}/${projectName}/debug/debug_%d{yyyy-MM-dd}_%i.log
</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
<!-- 超过50MB再按日期生成新的文件 -->
<maxFileSize>${maxFileSize}</maxFileSize>
</rollingPolicy>
<!-- 追加方式记录日志 -->
<append>true</append>
<encoder>
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> -->
<!-- logback的MDC功能。traceId变量来自于上面LogInterceptor拦截器下的traceId。-->
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%X{traceId}] [%logger] %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="FILE_INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- SizeAndTimeBasedRollingPolicy:日志按时间和大小滚动 -->
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- %d{yyyy-MM-dd}:每天的日期格式,%i:序号,从0开始 -->
<fileNamePattern>${logPath}/${projectName}/info/info_%d{yyyy-MM-dd}_%i.log
</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
<!-- 超过50MB再按日期生成新的文件 -->
<maxFileSize>${maxFileSize}</maxFileSize>
</rollingPolicy>
<!-- 追加方式记录日志 -->
<append>true</append>
<encoder>
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> -->
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%X{traceId}] [%logger] %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="FILE_WARN"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- SizeAndTimeBasedRollingPolicy:日志按时间和大小滚动 -->
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- %d{yyyy-MM-dd}:每天的日期格式,%i:序号,从0开始 -->
<fileNamePattern>${logPath}/${projectName}/warn/warn_%d{yyyy-MM-dd}_%i.log
</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
<!-- 超过50MB再按日期生成新的文件 -->
<maxFileSize>${maxFileSize}</maxFileSize>
</rollingPolicy>
<!-- 追加方式记录日志 -->
<append>true</append>
<encoder>
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> -->
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%X{traceId}] [%logger] %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="FILE_ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- SizeAndTimeBasedRollingPolicy:日志按时间和大小滚动 -->
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- %d{yyyy-MM-dd}:每天的日期格式,%i:序号,从0开始 -->
<fileNamePattern>${logPath}/${projectName}/error/error_%d{yyyy-MM-dd}_%i.log
</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
<!-- 超过50MB再按日期生成新的文件 -->
<maxFileSize>${maxFileSize}</maxFileSize>
</rollingPolicy>
<!-- 追加方式记录日志 -->
<append>true</append>
<encoder>
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> -->
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%X{traceId}] [%logger] %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="ASYNC_LOG_DEBUG"
class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>${queueSize}</queueSize>
<appender-ref ref="FILE_DEBUG"/>
</appender>
<appender name="ASYNC_LOG_INFO"
class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>${queueSize}</queueSize>
<appender-ref ref="FILE_INFO"/>
</appender>
<appender name="ASYNC_LOG_WARN"
class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>${queueSize}</queueSize>
<!-- 添加附加的appender,最多只能添加一个 -->
<appender-ref ref="FILE_WARN"/>
</appender>
<appender name="ASYNC_LOG_ERROR"
class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>${queueSize}</queueSize>
<!-- 添加附加的appender,最多只能添加一个 -->
<appender-ref ref="FILE_ERROR"/>
</appender>
<!-- 设置Spring、mybatis、jdbc日志输出级别 -->
<!-- additivity属性为false,表示此loger的打印信息不再向上级传递 -->
<logger name="org.springframework" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ASYNC_LOG_INFO"/>
</logger>
<!-- mybatis映射文件的日志输出级别 -->
<!--<logger name="com.admin.mapper" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ASYNC_LOG_DEBUG"/>
<appender-ref ref="ASYNC_LOG_INFO"/>
<appender-ref ref="ASYNC_LOG_WARN"/>
<appender-ref ref="ASYNC_LOG_ERROR"/>
</logger>-->
<root level="${logLevel}">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ASYNC_LOG_DEBUG"/>
<appender-ref ref="ASYNC_LOG_INFO"/>
<appender-ref ref="ASYNC_LOG_WARN"/>
<appender-ref ref="ASYNC_LOG_ERROR"/>
</root>
</configuration>