Using java.util.logging with Apache Karaf

The Situation

You have bundles that use JDK logging (java.util.logging).
These bundles are deployed in Apache Karaf and you want to configure the loggers for your bundles.

The solution

Edit the org.ops4j.pax.logging.cfg file under Karaf’s etc directory. You will configure your loggers with a Log4J configuration. Karaf (Pax Logging in fact) will make the bridge and configure your loggers through this configuration.

This allows your loggers to benefit from Karaf’s features, such as dynamically configuring your loggers just by editing and saving this file. And this is really cool.

Example

In a project I am working on, all my classes are located in packages that start with net.roboconf. Loggers are JDK loggers, I have no dependency to Log4J in my project. Here is how I configured the logging for these specific classes.

log4j.logger.net.roboconf = INFO, roboconf
log4j.appender.roboconf = org.apache.log4j.DailyRollingFileAppender
log4j.appender.roboconf.datePattern = '.'yyyy-MM-dd
log4j.appender.roboconf.layout = org.apache.log4j.EnhancedPatternLayout
log4j.appender.roboconf.layout.ConversionPattern = %d{ISO8601} | %-5.5p | %-16.16c{1} | %m%n
log4j.appender.roboconf.file = ${karaf.data}/log/roboconf.log
log4j.appender.roboconf.append = true

I simply added this snippet at the end of my *.cfg file, and that’s it! The only thing to be careful about is the name of the levels. FINE, FINER and FINEST do not exist with Log4J. You can find an equivalence on Pax’s web site.


About this entry