Friday, 12 March 2010

OSGi Tips&Tricks: Logging. OSGi to JUL. Forward OSGi logs to java.util.logging

Morning everybody,

There is a small tip - how to forward log entries from OSGi LogService to java.util.logging (JUL).
Steps that you have to do:
  1. Get org.osgi.service.log.LogReaderService from OSGi registry
  2. Add your implementation of org.osgi.service.log.LogListener interface to LogReaderService
  3. Initialize/get Java util Logger instance
  4. Implement LogListener method public void logged(LogEntry) to map OSGi LogEntry instance to Java LogRecord and publish it with Logger.

public void logged(LogEntry logEntry) {
    LOG.log(mapLogEntry(logEntry));
}

You can configure name on Java Logger with system property "org.osgilab.tips.logs.osgi2jul.loggerName" with my reference implementation.

Have a nice day!

1 comment:

  1. Excellent article , you have indeed covered topic in details with code examples and explanation. I have also blogged some of my experience as 10 tips on logging in Java

    Thanks
    Javin

    ReplyDelete