Previous Topic

Next Topic

Book Contents

Book Index

Log Reader Service

The Log Bundle provides the Log Reader Service for delivering log entries.

Overview

The Log Reader Service is defined by the OSGi Log Service Specification and provides access to the log entries, written in the Log Service. Bundle developers are allowed to get past log entries and to receive notifications about new ones.

There are two mechanisms for retrieval of log entries:

To prevent from potential memory leaks, the exceptions of a LogEntry are provided as special Throwable wrappers. The wrappers implement the Throwable methods defined in JDK 1.4 and do not provide support for the ones defined in JDK 1.5+ (e.g. getStackTrace()). Hence, to print the stack trace of the logged exception, use either of the printStackTrace methods.

Registering Bundle

The service is registered by OSGi Log Bundle. For more details refer to the System and Configuration properties of the bundle.

Using the Log Reader Service

The example below illustrates the usage of the getLog method. On each LogEntry the getMessage method is called to obtain its human readable message and the getLevel method – to have its severity level.

Listing 1. Using the Log Reader Service's getLog method and LogEntry's methods for retrieving information.

                             . . .

import org.osgi.service.log;
                             . . .

  private LogReaderService logReaderService;
                             . . .
  public void get(){
    LogEntry logEntry;

    // Puts logs in an enumeration
    Enumeration enum = logReaderService.getLog();

    while (enum.hasMoreElements()) {
      logEntry = (LogEntry)enum.nextElement();

      // Prints messages with severity level
      System.out.println("Level:" + logEntry.getLevel() +
                         " - logEntry Message: " + logEntry.getMessage());
    }
  }
                             . . .