Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do LogService and LogReaderService in Equinox work along?

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:

OSGi Log Service

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

like image 795
AlejandroVK Avatar asked Aug 17 '12 12:08

AlejandroVK


2 Answers

To answer the question directly:

A LogService is a service responsible for storing log messages. A LogReaderService is a service responsible for reading these log messages and dispatching them to log listeners. The binding between these is done automatically. What you will do yourself is log messages to the LogService on the one side, and possibly bind LogListeners to the LogReaderService, that will write out the logs somewhere, for example the console, on the other side.


To solve your problem of the logs not appearing, you need to do a few additional things.

First of all, did you install a bundle offering an implementation of the LogService and the LogReaderService into your osgi container?

You can check for the presense of an org.osgi.service.log.LogService by adding something like this to your Activator:

if(logService != null){ 
        System.out.println("There is a LogService available"); 
        logService.log(LogService.LOG_INFO, "Yee ha, I'm logging!"); 
} 
else { 
        System.out.println("There is no LogService available"); 
}

Or just type "bundles" in the equinox console and look for a bundle offering an org.osgi.service.log.LogService and an org.osgi.service.log.LogReaderService.

If no LogService is available, install one. For example:

install http://oscar-osgi.sf.net/repo/log/log.jar

The org.apache.log4j equinox dependency offers such a service too.

Start and stop your own bundle again. It should now print "There is a LogService available".


Now your messages are logged to the LogService and processed by the LogReaderService, but still, since there might be no LogListeners registered with this service(depending on other bundles started),

You might need to have to add a LogListener yourself in your bundle Activator.

For an example bundle Activator who does this, check http://blog.kornr.net/index.php/2008/12/09/understanding-the-osgi-logging-service.

like image 144
Integrating Stuff Avatar answered Oct 23 '22 11:10

Integrating Stuff


Well, just in case someone else is interested in the subject. I found a really nice wiki in Google Code explaining how OSGi logging system works that is a 'must read':

Understanding OSGi Logging

Since I only want to use a log to print in console, the solution proposed there works for me. Only problem was finding an implementation of the LogService that worked as expected. The one pointed out in the article actually works, but implements version 1.1 while what current LogService specification is at version 1.3 as far as I know. So what I did was, downloaded the sources from the previous implementation and adapted that implementation to my taste and to latest version (1.3) specification. You can find sources here.

Obviously, you can also use yours or any others implementation of the LogService interfaces, up to you.

Besides, if you plan to use a back-end existing logging service, such as commons-logging, or sl4j, etc...I recommend these links, they provide great info about where to start:

Building backend OSGi logging overview

This was overkill for me, since all I wanted was a way to print messages in the OSGi console, but without having to use log4j, fragments and the like.

Hope somebody found this useful

Regards, Alex

like image 25
AlejandroVK Avatar answered Oct 23 '22 09:10

AlejandroVK