Posts

Showing posts with the label mdc

MDC logging with Apache Karaf and Camel

Image
MDC (Mapped Diagnostic Context) logging is an interesting feature to log contextual messages. It’s classic to want to log contextual messages in your application. For instance, we want to log the actions performed by an user (identified by an username or user id). As you have a lot of simultaneous users on your application, it’s easier to “follow” the log. MDC is supported by several logging frameworks, like log4j or slf4j, and so by Karaf (thanks to pax-logging) as well. The approach is pretty simple: You define the context using a key ID and a value for the key: MDC.put("userid", "user1"); You use the logger as usual, the log messages to this logger will be contextual to the context: logger.debug("my message"); After that, we can change the context by overriding the key: MDC.put("userid", "user2");logger.debug("another message"); Or you can remove the key, so to remove the context, and the log will be “global” (not local to