The Log Bundle provides the Log Service, which implements the OSGi Log Service Specification and extends it with new functionality.
Overview
The Log Service is developed according to the OSGi Log Service Specification and keeps information such as time, message, bundle caller, etc., in log entry objects. When writing a message to the Log Service, specify the attributes:
If a log is received with a log level different from the predefined ones, the Log Service still keeps the log in the queue. This allows bundles to define their own log levels if needed.
The LogIntrospectorPlugin interface returns the list of skipped bundle symbolic names.
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 Service
The example tries to open a socket. If this operation fails at the primary port, which here is the default HTTP port 80, a Warning log entry is created. Then we try to open socket on the secondary port (8080) and in case it is successful an Info log entry is created, otherwise Error is logged. In both Warning and Error entries, the exception that occurred is included in the log entry.
. . .
import org.osgi.service.log.LogService;
import java.net.ServerSocket;
import java.io.IOException;
public class LogServiceSample {
private LogService logService;
private ServerSocket serverSocket;
private final static int PRIMARY_PORT = 80;
private final static int SECONDARY_PORT = 8080;
. . .
public LogServiceSample(LogService logService) {
this.logService = logService;
}
. . .
public void test() {
logService.log(LogService.LOG_DEBUG, "Testing LogService...");
try {
serverSocket = new ServerSocket(port);
logService.log(LogService.LOG_INFO, "Test server started and listening at "
+ PRIMARY_PORT + " port!");
} catch (IOException ex) {
logService.log(LogService.LOG_WARNING,
"The primary port is in use. Trying to bind to secondary port "
+ SECONDARY_PORT, ex);
try {
serverSocket = new ServerSocket(sec_port);
logService.log(LogService.LOG_INFO, "Test Server started and now listening at "
+ SECONDARY_PORT + " port!");
} catch (IOException ioex) {
logService.log(LogService.LOG_ERROR, "Can't open server socket. Port "
+ SECONDARY_PORT + " is in use!", ioex);
}
}
}
}