SLF4J Logging in Eclipse Plugins
Join the DZone community and get the full member experience.
Join For Freedeveloping 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
-
select “help / install new software…”.
- 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/”.
- add the freshly installed “slf4j.api” to your manifest.mf.
- 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
- select “help / install new software…”.
- install the “equinox target components” from the eclipse update site.
-
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
step 2: configure pax logging
-
set the location for your log configuration in the “eclipse.ini” as “vmarg"
… -vmargs -xms40m -xmx512m -dbundles.configuration.location=<config-dir> …
- create a folder named “services” in the above “config-dir.”
-
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
- open the “console” view.
- select the “host osgi console.”
-
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
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!
Published at DZone with permission of Michael Schnell, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments