[osgi logging] part 5 – listen to OSGI log reader with Declarative Services (DS)

This is part 5 of my blogseries about ‘Logging in OSGI Enterprise Applications‘ – you’ll find all parts here.

Last year I blogged how to listen to LogEntries from OSGI LogReaderService using a ServiceTracker. In the meantime (since Galileo) Equinox DS (Declarative Services) are part of the SDK. It’s now much easier to add a Listener then before, where you had to write a ServiceTracker.

What’s our goal:

In the last parts I already described how to get all LogEntries from other bundles even if they use Log4J, Commons-Logging or JDK Logging: all Log Messages are catched from our SLF4J / Logback Implementation.

We’re living in OSGI world, so its usual that you also have the need to get the LogEntries logged from bundles using OSGI LogServices: We can read LogEntries using OSGI LogReaderService and simply add a LogListener.

Hint: If you’re in Eclipse – Equniox OSGI world, then we also have to add the ExtendedLogReaderService to get the ExtendedLogEntry.

listen to logreader RT DS

With a little help of PDE its easy to create a DS Component:

DS component logservice

then we have to add References to LogReaderServices and ExtendedLogReaderServices:

DS referenced LogReaderService

In your Component Class you add / remove the LogListeners:

public void addLogReaderService(LogReaderService aLogReaderService) {
	NLogListener listener = new NLogListener();
	logReaderMap.put(aLogReaderService, listener);
	aLogReaderService.addLogListener(listener);
}
...
public void addExtendedLogReaderService(ExtendedLogReaderService aLogReaderService) {
	XLogListener listener = new XLogListener();
	extendedLogReaderMap.put(aLogReaderService, listener);
	aLogReaderService.addLogListener(listener);
}
...
private class NLogListener implements LogListener {
 	public void logged(LogEntry entry) {
	    // do the logging to SLF4J
   	}
}
...

At the end of this blog series all Bundles will be provided with Source, so you can take a look at the Source to see what happened in detail.

If you’re new to DS here’s Neil Bartletts Introduction to DS. You can also use Spring DM or iPOJO – see Heiko Seebergers comparision. So its up to you if you like ServiceTracker, DS, DM, iPOJO,….

In this blog entry I only want to give you one example how to implement LogListeners easy and how to log the catched LogEntries to SLF4J / Logback.

There will be more posts from me about the use of Declarative Services in redView and openerpware later in other blogs – this blog series has the focus on ‘Logging in OSGI Enterprise Applications’ the overview can be found here. stay tuned… part6 follows…

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

%d bloggers like this: