Class Logger
- java.lang.Object
-
- org.apache.felix.framework.Logger
-
- All Implemented Interfaces:
java.util.EventListener
,ServiceListener
public class Logger extends java.lang.Object implements ServiceListener
This class mimics the standard OSGi LogService interface. An instance of this class is used by the framework for all logging. By default this class logs messages to standard out. The log level can be set to control the amount of logging performed, where a higher number results in more logging. A log level of zero turns off logging completely.
The log levels match those specified in the OSGi Log Service (i.e., 1 = error, 2 = warning, 3 = information, and 4 = debug). The default value is 1.
This class also uses the System Bundle's context to track log services and will use the highest ranking log service, if present, as a back end instead of printing to standard out. The class uses reflection to invoking the log service's method to avoid a dependency on the log interface.
-
-
Field Summary
Fields Modifier and Type Field Description static int
LOG_DEBUG
static int
LOG_ERROR
static int
LOG_INFO
static int
LOG_WARNING
private static int
LOGGER_METHOD_IDX
private static int
LOGGER_OBJECT_IDX
private BundleContext
m_context
private java.lang.Object[]
m_logger
private int
m_logLevel
private ServiceReference
m_logRef
-
Constructor Summary
Constructors Constructor Description Logger()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
_log(Bundle bundle, ServiceReference sr, int level, java.lang.String msg, java.lang.Throwable throwable)
private void
_logReflectively(java.lang.Object[] logger, ServiceReference sr, int level, java.lang.String msg, java.lang.Throwable throwable)
protected void
doLog(Bundle bundle, ServiceReference sr, int level, java.lang.String msg, java.lang.Throwable throwable)
int
getLogLevel()
void
log(int level, java.lang.String msg)
void
log(int level, java.lang.String msg, java.lang.Throwable throwable)
void
log(Bundle bundle, int level, java.lang.String msg)
void
log(Bundle bundle, int level, java.lang.String msg, java.lang.Throwable throwable)
void
log(ServiceReference sr, int level, java.lang.String msg)
void
log(ServiceReference sr, int level, java.lang.String msg, java.lang.Throwable throwable)
void
serviceChanged(ServiceEvent event)
This method implements the callback for the ServiceListener interface.private void
setLogger(java.lang.Object logObj)
This method sets the new log service object.void
setLogLevel(int i)
protected void
setSystemBundleContext(BundleContext context)
private void
startListeningForLogService()
This method is called when the system bundle context is set; it simply adds a service listener so that the system bundle can track log services to be used as the back end of the logging mechanism.
-
-
-
Field Detail
-
LOG_ERROR
public static final int LOG_ERROR
- See Also:
- Constant Field Values
-
LOG_WARNING
public static final int LOG_WARNING
- See Also:
- Constant Field Values
-
LOG_INFO
public static final int LOG_INFO
- See Also:
- Constant Field Values
-
LOG_DEBUG
public static final int LOG_DEBUG
- See Also:
- Constant Field Values
-
m_logLevel
private int m_logLevel
-
m_context
private BundleContext m_context
-
LOGGER_OBJECT_IDX
private static final int LOGGER_OBJECT_IDX
- See Also:
- Constant Field Values
-
LOGGER_METHOD_IDX
private static final int LOGGER_METHOD_IDX
- See Also:
- Constant Field Values
-
m_logRef
private ServiceReference m_logRef
-
m_logger
private java.lang.Object[] m_logger
-
-
Method Detail
-
setLogLevel
public final void setLogLevel(int i)
-
getLogLevel
public final int getLogLevel()
-
setSystemBundleContext
protected void setSystemBundleContext(BundleContext context)
-
log
public final void log(int level, java.lang.String msg)
-
log
public final void log(int level, java.lang.String msg, java.lang.Throwable throwable)
-
log
public final void log(ServiceReference sr, int level, java.lang.String msg)
-
log
public final void log(ServiceReference sr, int level, java.lang.String msg, java.lang.Throwable throwable)
-
log
public final void log(Bundle bundle, int level, java.lang.String msg)
-
log
public final void log(Bundle bundle, int level, java.lang.String msg, java.lang.Throwable throwable)
-
doLog
protected void doLog(Bundle bundle, ServiceReference sr, int level, java.lang.String msg, java.lang.Throwable throwable)
-
_log
private void _log(Bundle bundle, ServiceReference sr, int level, java.lang.String msg, java.lang.Throwable throwable)
-
_logReflectively
private void _logReflectively(java.lang.Object[] logger, ServiceReference sr, int level, java.lang.String msg, java.lang.Throwable throwable)
-
startListeningForLogService
private void startListeningForLogService()
This method is called when the system bundle context is set; it simply adds a service listener so that the system bundle can track log services to be used as the back end of the logging mechanism. It also attempts to get an existing log service, if present, but in general there will never be a log service present since the system bundle is started before every other bundle.
-
serviceChanged
public final void serviceChanged(ServiceEvent event)
This method implements the callback for the ServiceListener interface. It is public as a byproduct of implementing the interface and should not be called directly. This method tracks run-time changes to log service availability. If the log service being used by the framework's logging mechanism goes away, then this will try to find an alternative. If a higher ranking log service is registered, then this will switch to the higher ranking log service.- Specified by:
serviceChanged
in interfaceServiceListener
- Parameters:
event
- TheServiceEvent
object.
-
setLogger
private void setLogger(java.lang.Object logObj)
This method sets the new log service object. It also caches the method to invoke. The service object and method are stored in array to optimistically eliminate the need to locking when logging.
-
-