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:* log4j.throwableRenderer=org.apache.log4j.OsgiThrowableRenderer # File appender log4j.appender.out=org.apache.log4j.RollingFileAppender log4j.appender.out.layout=org.apache.log4j.PatternLayout log4j.appender.out.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n log4j.appender.out.file=${karaf.base}/log/karaf.log log4j.appender.out.append=true log4j.appender.out.maxFileSize=10MB log4j.appender.out.maxBackupIndex=100
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.
Tip
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:* log4j.throwableRenderer=org.apache.log4j.OsgiThrowableRenderer # File appender #log4j.appender.out=org.apache.log4j.RollingFileAppender #log4j.appender.out.layout=org.apache.log4j.PatternLayout #log4j.appender.out.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n #log4j.appender.out.file=${karaf.data}/log/karaf.log #log4j.appender.out.append=true #log4j.appender.out.maxFileSize=1MB #log4j.appender.out.maxBackupIndex=10
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 http://felix.apache.org/site/apache-felix-file-install.html) 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.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support 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.