Eclipse Yoxos Services Downloads Blogs About
Home > Blogs >

on Mar 24th, 2009OSGi Log Service

The OSGi specification defines a log service (Section 101.1) in the Service Compendium. Like most logging facilities, the log service allows you to specify a message, exception, log level and service reference to be logged. The log service can be acquired using typical OSGi service acquisition mechanisms like a ServiceTracker (see snippet below). However, I highly recommend that you look at using Declarative Services (see Section 112) if you’re working with OSGi services.

public class Activator implements BundleActivator {

	private ServiceTracker logServiceTracker;
	private LogService logService;

	public void start(BundleContext context) throws Exception {
		// 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!");
	}

	public void stop(BundleContext context) throws Exception {
		if(logService != null)
			logService.log(LogService.LOG_INFO, "Yee ha, I'm logging!");

		// close the service tracker
		logServiceTracker.close();
		logServiceTracker = null;
	}
}

Once acquired, you can log messages using the various utility methods LogService:

public interface LogService {
	public static final int	LOG_ERROR	= 1;
	public static final int	LOG_WARNING	= 2;
	public static final int	LOG_INFO	= 3;
	public static final int	LOG_DEBUG	= 4;
	public void log(int level, String message);
	public void log(int level, String message, Throwable exception);
	public void log(ServiceReference sr, int level, String message);
	public void log(ServiceReference sr, int level, String message,
			Throwable exception);
}

The messages are logged as LogEntry objects.

public interface LogEntry {
     public Bundle getBundle();
     public ServiceReference getServiceReference();
     public int getLevel();
     public String getMessage();
     public Throwable getException();
     public long getTime();
}

It’s that simple! The problem with this simplicity is that this type of logging may not be enough for certain projects. The Equinox project at Eclipse has recently graduated an extended log service. The extended log service is similar to the log service we discussed, but there are a few useful additions like named loggers:

public interface ExtendedLogService extends LogService, Logger {
	public Logger getLogger(String loggerName);
	public Logger getLogger(Bundle bundle, String loggerName);
}
public interface Logger {
	public void log(int level, String message);
	public void log(int level, String message, Throwable exception);
	public void log(ServiceReference sr, int level, String message);
	public void log(ServiceReference sr, int level, String message, Throwable exception);
	public void log(Object context, int level, String message);
	public void log(Object context, int level, String message, Throwable exception);
	public boolean isLoggable(int level);
	public String getName();
}

There is also an extended log entry that has information like a logger name and thread-related information:

public interface ExtendedLogEntry extends LogEntry {
	String getLoggerName();
	Object getContext();
	long getThreadId();
	String getThreadName();
	long getSequenceNumber();
}

That’s it!

For those working in RCP or OSGi projects, how are you guys handling logging? Are you using the LogService, Pax Logging or something more exotic?

Related posts:

6 Responses to “OSGi Log Service”

  1. David Pinn says:

    Rather than doing logging explitly, I use StatusManager with the StatusManager.LOG style. I don’t know if that’s the right thing to use; please advise.

  2. For an RCP project I’m participating in we use an in house logger framework (on top of commons.logging) that is used both server and client side. The backend is Log4J, so I created an abstract LoggingPlugin that sets up an appender to the current plugin’s Log. Every logging framework needs to create its own set of levels with either 3, 4, 5 or 7 levels :-(
    Can’t you add a TRACE level to LogService as well? At least than we can adapt between all of them.

  3. Ralf Ebert says:

    For RCP apps I’d prefer if you could use a simple logging facade like slf4j with the Eclipse logging services as logging backend, because I like the simple and clean API ( http://www.slf4j.org/manual.html ). It’s logging, it should be dead simple: declare a logger for your class in one line and use it in another line.

    I tried to integrate that once, but it’s not so easy because this of course has no idea about bundles. I never wanted to know which “bundle” wrote a log entry, but it’s part of the Eclipse/OSGi logging framework and you couldn’t supply this information without extending slf4j’s api (to pass in the BundleContext for example) or resolving the bundle from the class… I’ll have to investigate that again :)

  4. Thomas Moore says:

    Maybe you can help me. I’ve created an OSGi service in Eclipse that obtains an instance of the LogService via declarative services. I can now call LogService.log as much as I want. However, I can’t figure out how on earth to actually view the log and/or cause log entries to be displayed to the console. Any ideas?

  5. Peter says:

    –Maybe you can help me. I’ve created an OSGi service in Eclipse that obtains an instance of the LogService via declarative services. I can now call LogService.log as much as I want. However, I can’t figure out how on earth to actually view the log and/or cause log entries to be displayed to the console. Any ideas?–

    I had the same problem with mbs implementation of OSGi. I figured out that you have to type “log” into the console.

  6. Rhett says:

    It seems to me I’ve got the log service and logged info with it. but I cannot find where the log entries are.
    Also, I don’t know how to tell the log server where to write the log file, can you help on this? Thanks.

© EclipseSource 2008 - 2011