SLF4J Logging in Eclipse Plugins

Developing with Maven and pure Java libraries all the time, I never thought it could be a problem to issue a few log statements when developing an Eclipse plugin. But it looks like in the imaginary of an Eclipse developer everything is always inside the Eclipse environment and nothing is outside the Eclipse universe.

If you search for the above headline using Google, one of the first articles you’ll find is one about the “platform logging facility”. But what about 3rd libraries? They cannot use an Eclipse-based logging framework.

In my libraries I use the SLF4J API and leave it up to the user to decide what logging implementation (Log4J, Logback, JDK) he or she wants to use. And that’s exactly what I want to do in Eclipse. It was hard to figure out exactly how to do it, but here are the pieces of that puzzle.

Phase 1: Development

This describes the steps during the development phase of your custom plugin.

Step 1: Get your libaries into a P2 repository

Everything you want to use in Eclipse has to be installed from a P2 repository. But most of the libaries I use are in a Maven repository. As far as I know there is no such thing as a main P2 repository similar to the “Maven Central,” and all libraries I found in P2 repositories were pretty old. So you have to create one by yourself.

Luckily there is a Maven plugin called p2-maven-plugin that converts all your Maven JARs into a single P2 repository. You can upload the plugin to a folder of your website or simply install it from your local hard drive.

For this example you’ll need the following libraries:

  • org.slf4j:slf4j-api:1.6.6
  • org.slf4j:slf4j-log4j12:1.6.6
  • log4j:log4j:1.2.17
  • org.ops4j.pax.logging:pax-logging-api:1.7.0
  • org.ops4j.pax.logging:pax-logging-service:1.7.0
  • org.ops4j.pax.confman:pax-confman-propsloader:0.2.2

Format “groupId:artifactid:version” is as used for the “p2-maven-plugin.” To skip this step you could also use http://www.fuin.org/p2-repository/.

Step 2: Install the SLF4J API in the Eclipse IDE

  1. Select “Help / Install New Software…”.
    Eclipse / Help / Install
  2. Add the P2 repository URL and install the “slf4j-api”—you could directly use the folder from Step 1 with a file URL like this: “file:/pathtoyour/p2-repository/”.Instal Slf4J API
  3. Add the freshly installed “slf4j.api” to your MANIFEST.MF.Dependencies in MANIFEST.MF
  4. Start using SLF4J logs in your code as usual.

Phase 2: Production

This describes the tasks a user of your custom plugin has to complete to start logging with Log4J. The following assumes that your custom plugin is already installed.

Step 1: Install the log libraries in the Eclipse IDE

  1. Select “Help / Install New Software…”.Eclipse / Help / Install
  2. Install the “Equinox Target Components” from the Eclipse Update Site.Install Equinox Target Components
  3. Add the P2 repository URL and install the following plugins:
    • Apache Log4j
    • OPS4J Pax ConfMan–Properties Loader
    • OPS4J Pax Logging–API
    • OPS4J Pax Logging–Service

    Install Log Libs

Step 2: Configure PAX Logging

  1. Set the location for your log configuration in the “eclipse.ini” as “vmarg"

    -vmargs
    -Xms40m
    -Xmx512m
    -Dbundles.configuration.location=<config-dir>

  2. Create a folder named “services” in the above “config-dir.”
  3. Create Log4J properties named “org.ops4j.pax.logging.properties” in “services.”

    log4j.rootLogger=INFO, FILE
    log4j.appender.FILE=org.apache.log4j.FileAppender
    log4j.appender.FILE.File=<path-to-your-log>/example.log
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.FILE.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n
    log4j.logger.your.package=DEBUG

Step 3: Activate PAX Logging

  1. Open the “Console” view.Show Console View
  2. Select the “Host OSGI Console.”Select OSGI Console
  3. Start the following bundles:
    start org.eclipse.equinox.cm
    start org.ops4j.pax.logging.pax-logging-api
    start org.ops4j.pax.logging.pax-logging-service
    start org.ops4j.pax.configmanager

    Start logging bundles

Now you should be able to see your log statements in the configured “example.log” file.

Step 4: Changing the configuration

If you want to change the configuration in the “org.ops4j.pax.logging.properties”, simply restart the PAX Configmanager in the OSGI console:

stop org.ops4j.pax.configmanager
start org.ops4j.pax.configmanager

Happy Logging!

About these ads

2 Responses to “SLF4J Logging in Eclipse Plugins”

  1. Krzysztof Says:

    Nice tutorial. For viewing logs you can use OtrosLogViewer ( http://code.google.com/p/otroslogviewer/)

  2. Michael Says:

    Tnx. Otros looks promising.

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: