我想为每个请求添加一些相关性ID,并将其自动显示在日志消息中。如何自动为<logger />及其变体添加额外的值?

我努力了:


Spring aop切成LoggerMessageProcessor.process(MuleEvent event),但该事件不包含记录的消息
Spring aop切成LoggerMessageProcessor.setMessage(String msg)会包含消息,但是由于某种原因未调用该方法

最佳答案

<flow name="add-correlation-id">
        <scripting:component doc:name="Script">
        <scripting:script engine="groovy">
            <![CDATA[
                String correlationId=message.getInboundProperty('x-request-id');
                if(correlationId==null || correlationId.length() == 0){
                    correlationId = java.util.UUID.randomUUID().toString();
                }
                message.setSessionProperty('requestID',correlationId);
                org.apache.log4j.MDC.put('x-request-id',correlationId);
            ]]>
        </scripting:script>
    </scripting:component>
</flow>

09-30 11:43