I'm developing a OSGi Equinox bundle and I'd like to add some logging to it, mostly to be redirected to the OSGi console, just for debugging purposes.
After discarding the usage of log4j, since there are a couple of logging services in Equinox (LogService and ExtendedLogService), I found this article describing how to use the LogService:
So I came up with an Activator that looks like this:
package org.example.servlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;
import org.eclipse.equinox.log.ExtendedLogService;
public class Activator implements BundleActivator {
private static BundleContext context;
private ServiceTracker logServiceTracker;
private LogService logService;
static BundleContext getContext() {
return context;
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
// create a tracker and track the log service
logServiceTracker = new ServiceTracker(context, LogService.class.getName(), null);
logServiceTracker.open();
// grab the service
logService = (LogService) logServiceTracker.getService();
if(logService != null)
logService.log(LogService.LOG_INFO, "Yee ha, I'm logging!");
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
Well, I never see the logged message in the OSGi console...looking for some more info, I found this thread:
How to use Equiniox's LogService ?
Some answers suggest that I should implement a LogServiceReader object that actually listens for logging events and (this is just my guess), redirects logged messages to whatever (file, console, etc...)
Now my question, more than how to implement this interface, is how do I do the binding between my implementation of the LogServiceReader and the LogService used in the Activator...
Thanks! Alex