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:

<build>
<plugins>
<plugin>
<groupid>org.apache.felix</groupid>
<artifactid>maven-bundle-plugin</artifactid>
<version>2.3.5</version>
<extensions>true</extensions>
<configuration>
<instructions>
<import-package>!*</import-package>
<!-- embed all compile and runtime scope dependencies -->
<embed -Dependency>*;scope=compile|runtime;inline=true</embed>
<!-- the host for this fragment bundle -->
<fragment-Host>org.ops4j.pax.logging.pax-logging-service</fragment-Host>
</instructions>
</configuration>
</plugin>
</plugins>
</build>

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 startup.properties 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
#
org/ops4j/pax/url/pax-url-aether/1.3.4/pax-url-aether-1.3.4.jar=5
org/ops4j/pax/url/pax-url-wrap/1.3.4/pax-url-wrap-1.3.4.jar=5
org/ops4j/logging/Extra-Fragment/1.0.0-SNAPSHOT/Extra-Fragment-1.0.0-SNAPSHOT.jar=7
org/ops4j/pax/logging/pax-logging-api/1.6.3/pax-logging-api-1.6.3.jar=8
org/ops4j/pax/logging/pax-logging-service/1.6.3/pax-logging-service-1.6.3.jar=8

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

the systems folder of Karaf.
$karaf.home/system/org/ops4j/logging/Extra-Fragment/1.0.0-SNAPSHOT

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=org.ops4j.pax.logging.extender.ZipRollingFileAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n
log4j.appender.out.file=${karaf.data}/log/karafgz.log
log4j.appender.out.append=true
log4j.appender.out.maxFileSize=2kB < -- just to see if the logrotating works
log4j.appender.out.maxBackupIndex=10

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


Beitrag veröffentlicht

in

,

von

Schlagwörter:

Kommentare

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
    Bram

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

    Bundle-Version: 1.0.0.${tstamp}
    -buildpath: \
    org.ops4j.pax.logging.pax-logging-service
    Export-Package: \
    com.company.logging
    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.