I wanted to use a more streamlined logging framework in my eclipse rcp plug development. First of I wanted to get rid of the verbose and hard-to-read existing logging code used in rcp plug-ins:
MyPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, MyPlugin.PLUGIN_ID, “Oh noes!”);
and instead use something more easy to read like:
I heard some good things about logback and started out to integrate it into my eclipse plug-ins. It turned out to be a non trivial operation to get everything working in a rcp application, so I decided to write this tutorial in order the make it easier for future generations.
Following the instructions from the logback site I got all my logs written in a rotating manner into a specific folder and also sent to stdout ,i.e. the console, without any problems. However to my surprise, and horror, I saw that all my logs where now excluded from the standard Eclipse “Error Log”-view.
There is however a specific logback eclipse plug-in http://logback.qos.ch/consolePlugin.html which provides a new log-view, but it is not up to date, and I don’t want to force my users to use a third party plug-in just to see the error/infor/warning logs.
So I wrote my own logback Eclipse Appender which catches all logs being made to logback and then duplicates them as “normal” eclipse logs.
Unfortunately when launching my plug-ins, logback complained that it could not find my custom eclipse appender.
ch.qos.logback.core.joran.action.AppenderAction - Could not create an Appender of type [se.oixx.EclipseAppender].
ch.qos.logback.core.util.DynamicClassLoadingException: Failed to
instantiate type se.oixx.EclipseAppender
The reason for this turns out that logback and my appender are loaded with different class loaders, which results in the Exception seen above.
In order to get this working I did the following:
”File -> New -> Project... -> Plug-in from Existing JAR Archives”
Browse to and add the three jar files (or the latest version)
Give the project a proper name e.g. “se.oixx.logback-libs”
Now open up your “MANIFEST.MF” file in our newly created project and add the following line:
This will make sense in a bit…
Now double click on your “MANIFEST.MF” and open up the “Runtime” tab.
In the “Classpath” section make sure that you add “ch/”, “.” and “org/”.
This way all of our plug-ins that want to log, as well as our Eclipse Appender that we soon will come to, will get access to logback and slf4j.
Now let’s create the Eclipse Appender:
“File -> New Project”, "Plug-in Project"
Name it e.g. “se.oixx.appenders”
We will only create one appender right now, but you never know if you want to create more in the future hence the plural on “appenders”.
Make sure that you check the “Generate an activator” option.
Now, double click the MANIFEST.MF file in your newly created plug-in project, “se.oixx.appenders”.
Select the page(tab) “Dependencies” and add the plug-in project containing our logback and slf4j binaries, “se.oixx.logback-libs” as a required plug-in.
Now to the trick which will let my appender be loaded with the very same class loader as the logback/slf4j binaries, thus making logback find my appender. Go to the “MANIFEST.MF” page(tab) and add the following line:
followed by a new empty line.
Now it’s finally time for some real Java code. Create a new class in your “se.oixx.appenders” project and name it “EclipseAppender.java”.
That’s it for eclipse configuration! Now we can point out our new appender from our logback.xml file like so:
<appender name="ECLIPSE" class="se.oixx.appenders.EclipseAppender"></appender>
The beauty is that this now works alongside other built-in logback appenders, such as the ch.qos.logback.core.ConsoleAppender
In order to actually start logging from other plug-in projects, add “se.oixx.logback-libs” as a required plug-in.
Import these two classes:
create your logging instance as a member
Logger mLog = LoggerFactory.getLogger(getClass());
That’s it, we are done! Happy Logging.