Logging RCP Applications With Pax Logging
Join the DZone community and get the full member experience.Join For Free
adding logging to an rcp application has always been painful. developers have struggled with the best way to incorporate log4j and other logging apis, and in particular with how to make configuration files accessible. some have chosen to use buddy classloading, others have utilized fragments containing the config files.
pax logging to the rescue
i’m happy to say that rcp developers now have another choice - pax logging . there are a few great things about this library:
- installation is easy. i’ll describe this in a bit.
- configuration files can be placed in regular folders, either inside of an application bundle or anywhere on a users machine.
- many logging apis are supported, including log4j, commons logging, jdk logging, slf4j and more. this means that legacy and third-party code can run as-is no matter what logging api they use.
logging configuration with pax confman
because pax logging requires the osgi configuration admin service to work, your first step is to install this service in the form of pax confman. in my last blog post i provided detailed instructions on how to do this.
adding configuration settings
the good news is that once you install pax confman, you’re almost done! pax confman requires the pax logging bundles in order to work, so we only have a few minor steps left.
the first is to add a logging configuration file. when using the osgi configuration admin service, services are identified using a persistent identifier , or pid . developers can choose the pids for their own services, and an implementation of the config admin uses these pids to inject properties into services.
pax confman does this by requiring that a properties file be named based on the pid of the service it is going to configure. for example, the pax logging pid is org.ops4j.pax.logging and so the properties file for this service will be called org.ops4j.pax.logging.properties.
if you created the folder structure suggested in the pax confman setup, simply create a file called org.ops4j.pax.logging.properties in the confadmin/services
directory. you can now place whatever logging configuration you like
into this file. for instance, here’s what i have for my simple test
log4j.appender.a1.layout.conversionpattern=%-4r [%t] %-5p %c %x - %m%n
forcing pax logging to start
just as we did with pax confman, its necessary to force pax logging to start up when our application is launched. to do this, we simply need add the org.ops4j.pax.logging.pax-logging-service bundle to the configuration page of the product configuration editor and set it’s auto-start property to true. if you’ve followed the instructions for installing pax confman, your complete list should look like this:
fire it up
that’s all there is to it. start up your rcp application and you should see log messages appear in your console.
Opinions expressed by DZone contributors are their own.