Adding Custom Log-Appender to Pax-Logging

Pax Logging does use log4j as the underlying service layer to actually produce the “log files”. This little tutorial shows you how to add your own Log-Appender to the underlying log4j infrastructure and actually use it.

As a new Appender we do a specialized appender of the Roling-File-Appender that actually also zips the file after it’s been rolled over. This Appender was inspired by this blog

Ok first of all create an appender that just looks like the one, the sources can be found here at GitHub. I’ll leave the way the appender works up to you, the interesting part is actually the configuration of the fragment bundle and later one the right configuration of your OSGi environment. I take the latest Karaf 3.0.0-SNAPSHOT version since it was lying around at my machine. But it should work right away with any other Karaf version.

Building the Fragment Bundle
The important part is in the maven pom as it can be seen in the little snippet below:

<!-- embed all compile and runtime scope dependencies -->
<embed -Dependency>*;scope=compile|runtime;inline=true</embed>
<!-- the host for this fragment bundle -->

After a build we’ll see that the MANIFEST contains a Fragment-Host entry

configuring Karaf
Since Karaf uses Pax Logging it is regarded to be system bundles. Therefore the logging bundle are among the first 10 configured bundles in the inside the etc folder of Karaf. Due to the fact that the system-bundle (either Felix or Equinox) don’t attach fragments to the host bundle after the host bundle is fully resolved and started we need to make sure our fragment is started beforehand. This can be achieved by placing the fragment before the pax-logging bundles.

# Startup core services like logging

To get Karaf working you need to place the fragment bundle at the exact position inside

the systems folder of Karaf.

using the new appender
Now everything is setup the way needed to get the appender going.

Just change the existing RollginFileAppender configuration to use the new ZipRollingFileAppender:

# File appender zipped
log4j.appender.out.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %X{} - %X{} - %X{bundle.version} | %m%n
log4j.appender.out.maxFileSize=2kB < -- just to see if the logrotating works

generate some logs and you’ll see that the newly created appender actually works.

Beitrag veröffentlicht






3 Antworten zu „Adding Custom Log-Appender to Pax-Logging“

  1. Avatar von Andrei Pozolotin
    Andrei Pozolotin

    is there a trick that allows to avoid hard coded ordering?

    like install fragment later on demand, then trigger host update via package admin?

  2. Avatar von achim

    Not really since it’s a resolver issue, the fragment must be known to the host and therefore you need to make sure it’s available before the resolving of the host bundle starts. What you later are still able to do is a refresh on the host bundle. Though doing this in a automagical way is not possible right now.

  3. Avatar von Bram

    Thanks for the information. For people interested in how this can be done with bndtools:

    Bundle-Version: 1.0.0.${tstamp}
    -buildpath: \
    Export-Package: \
    Fragment-Host: org.ops4j.pax.logging.pax-logging-service
    Embed-Dependency: *;scope=compile|runtime;inline=true
    Import-Package: !*
    Bundle-Name: Company Logging

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.