Logging RCP applications with Pax Logging

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:

paxlogging-1

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.

paxlogging-2

Happy logging!

About these ads

2 Responses to Logging RCP applications with Pax Logging

  1. Good to know someone appreciates that new Configuration page on the product definition editor ;)

  2. Philipp Kursawe says:

    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?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: