Propagating custom information in Log events
About this task
The MDC feature of existing logging frameworks like log4j, slf4j, and so on, can be used to propagate custom or user information as metadata in the Log event. This allows custom or business code logs to contain a business correlation ID for example, or other important metadata in a structured form. The following Java code snippet demonstrates how a custom information can be added as MDC attribute in the log message:
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
public class SimpleMDC {
static public void main(String[] args) throws Exception {
// You can put values in the MDC at any time. Before anything else
// we put the subject name
MDC.put("subjectName", "Joe");
[ SNIP ]
Logger logger = Logger.getLogger(SimpleMDC.class);
// We now put the last name
MDC.put("loggedInAs", "Admin");
logger.info("Check enclosed.");
MDC.put("myCorrelatonID", "154516516521");
logger.info("Using business correlationid");
}
[ SNIP ]
MDC.clear();
}
To get the MDC values printed in the logs, the log4j2.properties configuration must be adapted. For example:
# Root logger
log4j2.rootLogger.level = INFO
# uncomment to use asynchronous loggers, which require mvn:com.lmax/disruptor/3.3.2 library
#log4j2.rootLogger.type = asyncRoot
#log4j2.rootLogger.includeLocation = false
log4j2.rootLogger.appenderRef.RollingFile.ref = RollingFile
log4j2.rootLogger.appenderRef.PaxOsgi.ref = PaxOsgi
log4j2.rootLogger.appenderRef.Console.ref = Console
log4j2.rootLogger.appenderRef.Console.filter.threshold.type = ThresholdFilter
log4j2.rootLogger.appenderRef.Console.filter.threshold.level = ${karaf.log.console:-OFF}
If you have the above log4j configuration, the log output will look as follows:
09:33:57,203 | INFO | SimpleMDC | Joe Admin |Check enclosed.
09:33:57,204 | INFO | SimpleMDC | Joe Admin 154516516521|Using business correlationid
The Log -> Event conversion step will transform the log message format into the Event format, where essentially the Event Structure is filled in as completely as possible and the additional metadata, including the MDC attributes, are transformed in the Event Customer Information (Key, Value) list.
If you are in the Talend Studio, you can use the tJavaRow component to add your custom MDC attributes as shown by the image below:
To activate the MDC properties while using Mediation routes, the cConfig component can be used as follows:
Procedure
Results
For more information, go to: http://camel.apache.org/mdc-logging.html