Logging in Apache Karaf

Apache Karaf provides a powerful logging system based in OPS4j Pax Logging. Besides implementing a standard OSGi Log Service, it also supports all major logging APIs (Apache Commons Logging, SLF4J, Apache Log4j, Java Util Logging). So no matter what API your libraries are using, Karaf can log messages from them.

For new bundles I suppose that a good choice is SLF4J. This is a modern API that offers parameterized log messages. In Java code one usually creates a static logger:
 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Marketplace implements IMarketplace {

    private static Logger logger = LoggerFactory.getLogger(Marketplace.class);
    
}

Then the logger can be used anywhere in the class to log messages:

logger.debug("*** Activating Marketplace");
logger.error("There was an unexpected error: {}, ex.getMessage());

In order to use the above code in an OSGi bundle you need to import the org.slf4j in the Manifest.MF:
 

Import-Package: org.slf4j


In a bare OSGi container you would need to install the slf4j-api.jar (and also a logging framework like logback). In Karaf there isn't such a need, as the support for slf4j is built-in.

All logging messages are logged to data/log/karaf.log file. The configuration of the logging system uses a standard Log4j configuration file at etc/org.ops4j.pax.logging.cfg. Because Karaf would normally receives a lot of logging messages from different bundles, it would be good to be able to direct messages from a specific application to a separate logging file. This can be easily achieved by creating an appender.

The snippet below defines the aniketos appender, which will log all messages from the eu.aniketos package to the aniketos.log file.
 

log4j.logger.eu.aniketos=DEBUG, aniketos
log4j.additivity.eu.aniketos=false

# aniketos file appender
log4j.appender.aniketos=org.apache.log4j.RollingFileAppender
log4j.appender.aniketos.threshold=DEBUG
log4j.appender.aniketos.layout=org.apache.log4j.PatternLayout
log4j.appender.aniketos.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %X{bundle.name} | %m%n
log4j.appender.aniketos.file=${karaf.data}/log/aniketos.log
log4j.appender.aniketos.append=true
log4j.appender.aniketos.maxFileSize=1MB
log4j.appender.aniketos.maxBackupIndex=10


Apache Karaf offers a set of logging commands that allows you to monitor logging messages. For debugging it is however more convenient to follow the logging messages in a different window. In Windows one can use the baretail application. This an implementation of the UNIX tail command for the Windows operation system. It allows you to follow more than one logging files at the same time and apply highlighting to elements.

Batetail application

A logging framework is a much more powerful solution than using System.out.println(). Since in Karaf you only need to add a single line of code and don't need to install any additional bundles, you don't have an excuse for still using printlns.
 

ANIKETOS newsletter

Stay informed on our latest news!

Login

Only for users who has an user and a password sent by the administrator.