DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
...
The OSGi Service Compendium specification defines a general purpose Log Service for the OSGi Platform.
It is a very simple specification that doesn't provide all the functionalities functionality commonly available in enterprise-level logging tools, but its extensible service model can be used to build fairly sophisticated logging solutions.
The Log Service specification defines four main entities:
org.osgi.service.log.LogService: service interface to log informations. It's possible to set a log level (LOG_DEBUG, LOG_INFO, LOG_WARNING or LOG_ERROR), a message, an Exception information, including log level, message, exception, and theServiceReferencethat generated the log.org.osgi.service.log.LogReaderService: service interface to add and remove {{LogListener}}s and to retrieve recent log entries.org.osgi.service.log.LogEntry: interface defining a log entry.org.osgi.service.log.LogListener: interface defining a listener for log entries. A LogListener , which is notified about every new log entryentries.
Accessing the
...
log service
To access a LogService instance it is necessary to look it up in the OSGi service registry as demonstrated in the following code snippetIf the OSGi platform provides a Log Service, getting a reference to it is as simple as doing a normal OSGi service lookup:
| Code Block |
|---|
public class Activator implements BundleActivator
{
public void start(BundleContext context) throws Exception
{
ServiceReference ref = context.getServiceReference(LogService.class.getName());
if (ref != null)
{
{
LogService log = (LogService) context.getService(ref);
//use
// Use the log
}
...
}
}
//..
|
(Of course it's It is possible, and advisable, to use more sophisticated service acquisition mechanisms like a Service Tracker, Dynamic Services or iPOJO).
Using the
...
log service
The Log Service LogService interface provides four methods to define logsfor logging:
| Code Block |
|---|
public interface LogService
{
//..
// logLog a message specifying a log level
public log(int level, java.lang.String message)
//log Log an exception
public log(int level, java.lang.String message, java.lang.Throwable exception)
//log Log a message specifying the ServiceReference that generatesgenerated it
public log(ServiceReference sr, int level, java.lang.String message)
// Log a message specifying the ServiceReference and exception
public log(ServiceReference sr, int level, java.lang.String message, java.lang.Throwable exception)
}
|
Log levels are defined in the same interface:
LogService.LOG_DEBUGLogService.LOG_INFOLogService.LOG_WARNINGLogService.LOG_ERROR
Retrieving log entries
The LogReaderService provides a getLog() method to retrieve an Enumeration of the latest log entries. The following code snippets demonstrates how to retrieve it from the service registry and use it:
| Code Block |
|---|
ServiceReference ref = context.getServiceReference(LogReaderService.class.getName()); if (ref != null) { LogReaderService reader = (LogReaderService) context.getService(ref); Enumeration<LogEntry> latestLogs = reader.getLog(); } |
Creating and registering a LogListener
The Log Service specification doesn't define any particular entity to store, display, or write log entries; it's up to the developer to implement this functionality , or to choose an already available bundle implementation capable of doing that. To create such a bundle, the first step is to create an implementation of the LogListener interface. The following code shows a simple implementation that echoes the log message:
| Code Block |
|---|
public class LogWriter implements LogListener
{
// Invoked by the log service implementation for each log entry
public void logged(LogEntry entry)
{
//invoked by the OSGi platform every time a log entry is created
{
System.out.println(entry.getMessage());
}
}
|
The only method to implement is public void logged(LogEntry entry); this method will be method, which is called every time an installed bundle submits a log entry .The second and final step is to register the newly created LogListener is created in the associated logging service. A LogListener implementation must be registered with the LogReaderService so it can start receving receiving log entries, as demonstrated in the following code snippet:
| Code Block |
|---|
ServiceReference ref = context.getServiceReference(LogReaderService.class.getName()); if (ref != null) { LogReaderService reader = (LogReaderService) context.getService(ref); reader.addLogListener(new LogWriter()); } |
...
Property | Default | Description |
|---|---|---|
| 100 | The maximum size of the log used to maintain historic log informationhistory. A value of -1 means the log has no maximum size; a value of 0 means that no historic log historical information will be is maintained |
| false | Determines whether or not debug messages will be stored as part of the historic log informationin the history |