MDC logging does not work with camel.routeId when using exchange.addOnCompletion(...).

Solution Verified - Updated -

Issue

  • User migrated from camel 2.12 to 2.15 (Fuse 6.1 to Fuse 6.2) and noticed that his MDC logging didn't work anymore.
  • When user removed {camel.routeId} from the logging definition, all worked again.
  • Following are the logger configurations
log4j.appender.audit=org.apache.log4j.sift.MDCSiftingAppender
log4j.appender.audit.key=camel.contextId
log4j.appender.audit.default=unknown
log4j.appender.audit.appender=org.apache.log4j.RollingFileAppender
log4j.appender.audit.appender.layout=org.apache.log4j.PatternLayout
log4j.appender.audit.appender.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss,SSSXXX} | %-5.5p | %X{camel.routeId} %X{bundle.version} | %X{camel.breadcrumbId} | %X{messageType} | %m%n
log4j.appender.audit.appender.file=${karaf.data}/log/audit-$\\{camel.contextId\\}.log
log4j.appender.audit.appender.append=true
log4j.appender.audit.appender.maxFileSize=10MB
log4j.appender.audit.appender.maxBackupIndex=50
  • Following is the route
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
    xsi:schemaLocation="
           http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

        <bean id="myLogger" class="org.slf4j.LoggerFactory" factory-method="getLogger" >
                <argument value="auditLogger" />
        </bean>

        <bean id="onCompletionLogger" class="example.SimpleOnCompletionLogger">
                <argument ref="myLogger" />
        </bean>

    <camelContext id="myContextId" xmlns="http://camel.apache.org/schema/blueprint" useMDCLogging="true">
        <route id="myRouteId">
                <from uri="timer://foo?period=5s"/>
                <onCompletion>
                        <process ref="onCompletionLogger"/>
                </onCompletion>
                <log message="Logging test ${body}" loggerRef="myLogger"/>
        </route>
    </camelContext>
</blueprint>
  • Following way the processor is implemented
public class SimpleOnCompletionLogger implements Processor {
    private Logger logger;

    public SimpleOnCompletionLogger(Logger logger) {
        this.logger = logger;
    }

    @Override
    public void process(Exchange exchange) throws Exception {
        exchange.addOnCompletion(new Synchronization() {

            @Override
            public void onComplete(Exchange exchange) {
                // LoggerFactory.getLogger("auditLogger").info("onComplete");
                logger.info("onComplete");
            }

            @Override
            public void onFailure(Exchange exchange) {
                logger.info("onFailure");
            }
        });
    }
}
  • Logs are printed in following manner, here it can be seen that for one case routeId is not printed.
11:20:06,337 | INFO  | 72 - timer://foo | ROUTE_ID:myRouteId | auditLogger                      | Logging test
11:20:06,337 | INFO  | 72 - timer://foo | ROUTE_ID: | auditLogger                      | onComplete>

Environment

  • Red Hat JBoss Fuse
    • 6.2.0

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.