LogRef is an utility class that simplifies the use of the LogService.
LogRef let you use the log without worrying about getting new service objects when the log service is restarted. It also supplies methods with short names that does logging with all the different LogService severity types.
To use the LogRef you need to import org.knopflerfish.service.log.LogRef
and instantiate LogRef with your bundle context as parameter. The bundle context is used for getting the LogService and adding a service listener.
Example usage
The
if
statement that protects each call to the
LogRef
instance below is there to save the effort required for creating the message string object in cases where the log will throw away the log entry due to its unimportance. The user must have this
if
-test in his code since that is the only way to avoid constructing the string object. Placing it in the wrapper (LogRef) will not help due to the design of the Java programming language.
package org.knopflerfish.example.hello; import org.osgi.framework.*; import org.knopflerfish.service.log.LogRef; public class Hello implements BundleActivator { LogRef log; public void start(BundleContext bundleContext) { log = new LogRef(bundleContext); if (log.doInfo()) log.info("Hello started."); } public void stop(BundleContext bundleContext) { if (log.doDebug()) log.debug("Hello stopped."); } }
@author Gatespace AB
@see org.osgi.service.log.LogService
@see org.knopflerfish.service.log.LogService