Logging RCP applications with Pax Logging
July 14th, 2009 | Published in Rich Client Platform, Tutorial | 6 Comments
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 project:
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.logger.com.rcpquickstart.logtest=DEBUG
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.

Happy logging!



Patrick on the RCP Panel at EclipseCon
July 14th, 2009 at 5:15 pm (#)
Good to know someone appreciates that new Configuration page on the product definition editor ;)
July 15th, 2009 at 5:51 am (#)
I am using pax-logging since over a year now. Currently together with the fileinstall bundle that also feeds configurations to an CM available. It also has the benefit, that you can change the config during runtime and it will be updated.
I am just starting Pax-Logging at ST 1 to be sure to capture every logging message.
It would be nice if Eclipse would not write its own log file anymore. All the log informations should go to one log destination. Using the pax-logging service you can also attach to the eclipse logger and feed the Eclipse log entries to your logfile. Question is, how does one disable the creation of the Eclipse logfile?
October 16th, 2009 at 12:37 pm (#)
I am trying to use pax logging in my osgi application.
Since this is my first osgi application i do have some problems.
I want to use apache felix with pax logging. I tried to start the pax-logging-service with felix, but it fails to start. So in general: Waht do i have to do? Include pax-logging-api in my bundles where i want to log and start pax-logging-service (can i build the service for felix? Should it start up without problems? Can i wrap the service jar for felix`?)
Can someone please help me?
Greetings,
Niko
October 16th, 2009 at 12:47 pm (#)
Hi Niko,
Do you get an error message indicating why the logging bundle doesn’t start? I’m assuming you’ve installed Pax ConfMan as well.
BTW, if you’re targeting Felix you may just want to start with the Apache Karaf server. It’s basically Felix plus some standard services, including Pax Logging.
— Patrick
November 30th, 2009 at 12:45 pm (#)
I wonder if it is possible to configure Pax-Logging via fragment bundle and feed it a default config file?
November 30th, 2009 at 6:25 pm (#)
Hi Phil,
I haven’t looked into this myself, but it appears that Pax Logging provides a default log4j.properties file internally. This is mentioned in the following article:
http://wiki.ops4j.org/display/paxlogging/How+to+use+Pax+Logging
One option would be to simply open the bundle and modify the file by hand, but of course this would be a maintenance headache as new Pax Logging versions were released.
Placing the same file in a fragment would not work, as the bundle class path is searched before any fragment class paths.
Having said all this, there may be a way to do this that I’m not aware of. You may want to ask on the Pax mailing lists, and please do let me know if you find a good solution.
— Patrick