Book Image

Apache Karaf Cookbook

By : Jamie Goodyear, Johan Edstorm, Achim Nierbeck, Heath J Kesler
Book Image

Apache Karaf Cookbook

By: Jamie Goodyear, Johan Edstorm, Achim Nierbeck, Heath J Kesler

Overview of this book

Table of Contents (17 chapters)
Apache Karaf Cookbook
About the Authors
About the Reviewers

Configuring production-ready logging in Apache Karaf

One of the first tasks administrators of Apache Karaf undertake is changing the default logging configuration to more production-ready settings.

To improve the default logging configuration, we'll perform the following tasks:

  • Update the logfile location to be outside the data folder. This helps administrators avoid accidentally wiping out logfiles when deleting runtime data.

  • Increase the logfile size. The default size of 1 MB is too small for most production deployments. Generally, we set this to 50 or 100 MB, depending on the available disk space.

  • Increase the number of logfiles we retain. There is no correct number of logfiles to retain. However, when disk space is cheap and available, keeping a large number of files is a preferred configuration.

How to do it…

Configuring Karaf's logging mechanism requires you to edit the etc/org.ops4j.pax.logging.cfg file. Open the file with your preferred editor and alter the following highlighted code entries:

# Root logger
log4j.rootLogger=INFO, out, osgi:*

# File appender
log4j.appender.out.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X{} - %X{} - %X{bundle.version} | %m%n

In the preceding configuration, we instruct Karaf to write logs to a log folder in the base installation directory, increase the logfile size to 10 MB, and increase the number of retained logfiles to 100.

When finished editing the file, save the changes. They will take effect shortly.


We can change the verbosity of logging by altering the log4j.rootLogger entry from INFO to DEBUG, WARN, ERROR, or TRACE.

How it works…

The logging system for Karaf is based on OPS4J Pax Logging with the log4j library acting as its backend. The configuration file, etc/org.ops4j.pax.logging.cfg, is used to define appenders, log levels, and so on. Let's take a look at the following default appender configuration and how we'll tweak it to become more production-ready:

# Root logger
log4j.rootLogger=INFO, out, osgi:*

# File appender
#log4j.appender.out.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X{} - %X{} - %X{bundle.version} | %m%n

In the previous code, the File appender configuration sets up the default Karaf logging behavior. The initial configuration sets RollingFileAppender and constructs a log entry pattern. The remaining options dictate the location of the logfile, its size, and the number of logfiles to retain.

Karaf monitors the configuration file in the KARAF_HOME/etc folder. When the updates to the configuration file are read, the logging service is updated with the new value(s). The mechanism that allows this behavior is provided by File Install (available at and the OSGi Configuration Admin service. Have a look at the following figure:

As illustrated in the preceding figure, when a file in the KARAF_HOME/etc directory is created, deleted, or modified, the file scanner will pick up on the event. Given a configuration file change (a change in the file format of the Java properties file), a configuration processor will process the entries and update the OSGi Configuration Admin service.


Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

There's more…

To further improve logging, you can provide the log4j library with an external logging location, separating the I/O requirements of logging from the base system at the expense of increased network traffic. This architecture is shown in the following figure:

To achieve this logging architecture, you'll need to mount the external volume on the server on which Karaf is running.

See also

  • The Creating our own custom Karaf command using a Maven archetype recipe.