Monday, July 18, 2011

Karaf Logging Overview

A recent post on the Karaf User's mailing list was focused on the topic of Karaf logging. This topic is easily misunderstood, and can take some time to figure out. As is usual with many open-source items, this topic is only dryly documented. Below, hopefully I can help to alleviate this.

First, its good to remember that in log4j, logging can be set on a package level. This can be done in Karaf using the following console command:

karaf@root> log:set LEVEL mypackage.subpackage

This will set the logging for that specific package to whatever the level you provide.

In Karaf 3.x, you can also filter for a specific package using the following command:

karaf@root> log:get mypackage.subpackage

Some caveats with the log console commands. With the exception of log:set, your commands won't affect the logs placed on the file system. For example, if you type log:clear, you won't have access to any log messages written out prior to executing the log:clear command. However, log:clear won't remove those older log messages from the log directory.

This is because all of the logging commands don't actually work against your log files. Instead, the logging commands look for PaxLoggingEvents inside of Karaf. Anytime a log message is generated in Karaf, a PaxLoggingEvent is created. The logging commands look for these and then act on them. So, if you accidentally run log:clear, dont' worry, you won't lose all of your log messages. However, if you accidentally set your logging level to DEBUG or TRACE, this change will result in all of your logging messages being set to the new level and your logs filling up very quickly.

Now, the logging console commands are great, but what if you want to break your logging messages out into different log files? For the most part, the logging commands won't help you. If what you want is a file containing only the messages from a given package, you simply create a logger for your package, and then create a RollingFileAppender for that logger. Here's an example of what to place in your org.ops4j.pax.logging.cfg file.

# mypackage.subpackage appender
log4j.logger.mypackage.subpackage=LEVEL, subpackage

Now, some caveats. If your "subpackage" has camel routes that you'd like logged into your subpackage log file, they won't appear. This is because the messages from your camel routes are generated from the org.apache.camel package, and not by your subpackage.

Also, all messages are still going to be written into your karaf.log file. So, if you are seeing some strangeness and can't diagnose it in your subpackage.log file, check out your karaf.log.

As always, please let me know if you have any questions.

No comments:

Post a Comment