一、日志配置记录
日志组件
日志门面 日志实现 桥接方式 改变依赖 slf4j Logback slf4j-log4j12 jcl-over-slf4j commons-logging Log4j slf4j-jdk14 log4j-over-slf4j Log4j Log4j2 Java util logging
二、MDC具体应用
配置开启MDC【Mapped Diagnostic Contexts】
- 默认没有开启,需要手动设置LogMDC.setMDCEnable(true)
- 配置logback.xml日志模板 %X
- 流程只有执行过程出现异常时才会记录MDC信息
实现方式
一般情况,MDC只有在流程出现错误的情况,才会被捕获,打印到控制台。
可以使用拦截器的方式,来捕获MDC日志。
代码实现
使用拦截器
javapackage com.laogoubi.interceptor; import org.activiti.engine.impl.agenda.AbstractOperation; import org.activiti.engine.impl.interceptor.DebugCommandInvoker; import org.activiti.engine.logging.LogMDC; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @ClassName MDCCommandInvoker * @Description 捕获MDC日志 * @Author eastern * @Date 2020/6/28 下午11:10 * @Version 1.0 **/ public class MDCCommandInvoker extends DebugCommandInvoker { private static final Logger logger = LoggerFactory.getLogger(MDCCommandInvoker.class); @Override public void executeOperation(Runnable runnable) { // 获取LogMDC的状态 boolean mdcEnabled = LogMDC.isMDCEnabled(); // 无论是否打开,都打开 LogMDC.setMDCEnabled(true); if (runnable instanceof AbstractOperation) { AbstractOperation operation = (AbstractOperation) runnable; if (operation.getExecution() != null) { // 将可执行的对象放到上下文中 LogMDC.putMDCExecution(operation.getExecution()); } } // 执行正常的业务操作 super.executeOperation(runnable); // 清除上下文 LogMDC.clear(); // 还原现场,如果进来之前是不生效的,设置回原来的值 if (!mdcEnabled) { LogMDC.setMDCEnabled(false); } } }
xml配置activiti_mdc.cfg.xml
xml<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration"> <property name="commandInvoker" ref="commandInvoker"/> </bean> <bean id="commandInvoker" class="com.laogoubi.interceptor.MDCCommandInvoker"></bean> </beans>
日志依赖
xml<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.11</version> </dependency>
配置:logback.xml
xml<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="encoding" value="UTF-8"></property> <property name="mdc" value="%d{HH:mm:ss.SSS}[%thread][%-5level]%msg ProcessDefinitionId=%X{mdcProcessDefinitionID} executionId=%X{mdcExecutionId} mdcProcessInstanceID=%X{mdcProcessInstanceID} mdcBusinessKey=%X{mdcBusinessKey} %logger{10}.%M:%L%n"></property> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <!-- <layout class="ch.qos.logback.classic.PatternLayout">--> <!-- <!–输入合适–>--> <!-- <pattern>--> <!-- %d - %msg%n--> <!-- </pattern>--> <!-- </layout>--> <encoder> <pattern>${mdc}</pattern> <charset>${encoding}</charset> </encoder> </appender> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--过滤--> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <!--禁止--> <onMatch>DENY</onMatch> <!--接受--> <onMismatch>ACCEPT</onMismatch> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!--滚动策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路径--> <fileNamePattern>/Users/xxx/IdeaProjects/activiti-study/info.%d.log</fileNamePattern> </rollingPolicy> </appender> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--过滤--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!--滚动策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路径--> <fileNamePattern>/Users/xxx/IdeaProjects/activiti-study/error.%d.log</fileNamePattern> </rollingPolicy> </appender> <root level="info"> <appender-ref ref="consoleLog"></appender-ref> <appender-ref ref="fileInfoLog"></appender-ref> <appender-ref ref="fileErrorLog"></appender-ref> </root> </configuration>