Posts

Showing posts with the label pax-logging

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

Pax Logging: loggers log level

As you probably know, Apache Karaf uses Pax Logging as logging system. Pax Logging is an OPS4j project (Open Participation Software 4 Java) which provide a fully OSGi compliant framework for logging. Pax Logging leverages a bunch of logging frameworks like slf4j, logback, log4j, avalong, etc. It gathers all the configuration and the actual logging mechanisms in a central way. It means that, in your applications/bundles, you can use slf4j or log4j, it doesn’t matter, behind the hood you will use Pax Logging. Karaf provides a bunch of shell commands and MBean for logging: log:display to see the log log:display-exception to see only the exceptions log:tail to display and “follow on the fly” the log log:set to change the log level of a particular logger (or the rootLogger) log:get to get the current log level of a particular logger (or the rootLogger) The default configuration is a log4j configuration described in etc/org.ops4j.pax.logging.cfg. It’s where you especially define the loggers