public class LogRef extends Object implements org.osgi.framework.ServiceListener, 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. * *
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."); * } * } ** * *
*
,
LogService
LOG_DEBUG, LOG_ERROR, LOG_INFO, LOG_WARNING
Constructor and Description |
---|
LogRef(org.osgi.framework.BundleContext bc)
* Create new LogRef object for a given bundle.
|
LogRef(org.osgi.framework.BundleContext bc,
boolean out)
* Create new LogRef object for a given bundle.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
* Close this LogRef object.
|
void |
debug(String msg)
* Log a debug level message * *
|
void |
debug(String msg,
org.osgi.framework.ServiceReference sr)
* Log a debug level message.
|
void |
debug(String msg,
org.osgi.framework.ServiceReference sr,
Throwable e)
* Log a debug level message.
|
void |
debug(String msg,
Throwable e)
* Log a debug level message.
|
boolean |
doDebug()
* Returns true if messages with severity debug or higher * are saved by
the log.
|
boolean |
doError()
* Returns true if messages with severity error or higher * are saved by
the log.
|
boolean |
doInfo()
* Returns true if messages with severity info or higher * are saved by
the log.
|
protected void |
doLog(String msg,
int level,
org.osgi.framework.ServiceReference sr,
Throwable e)
* Sends a message to the log if possible.
|
boolean |
doWarn()
* Returns true if messages with severity warning or higher * are saved by
the log.
|
void |
error(String msg)
* Log an error level message.
|
void |
error(String msg,
org.osgi.framework.ServiceReference sr)
* Log an error level message.
|
void |
error(String msg,
org.osgi.framework.ServiceReference sr,
Throwable e)
* Log an error level message.
|
void |
error(String msg,
Throwable e)
* Log an error level message.
|
int |
getLogLevel()
* Returns the current log level.
|
void |
info(String msg)
* Log an info level message.
|
void |
info(String msg,
org.osgi.framework.ServiceReference sr)
* Log an info level message.
|
void |
info(String msg,
org.osgi.framework.ServiceReference sr,
Throwable e)
* Log an info level message.
|
void |
info(String msg,
Throwable e)
* Log an info level message.
|
void |
log(int level,
String message)
* Log a message.
|
void |
log(int level,
String message,
Throwable exception)
* Log a message with an exception.
|
void |
log(org.osgi.framework.ServiceReference sr,
int level,
String message)
* Log a message associated with a specific Service.
|
void |
log(org.osgi.framework.ServiceReference sr,
int level,
String message,
Throwable exception)
* Log a message with an exception associated with a specific Service.
|
void |
serviceChanged(org.osgi.framework.ServiceEvent evt)
* Service listener entry point.
|
void |
warn(String msg)
* Log a warning level message.
|
void |
warn(String msg,
org.osgi.framework.ServiceReference sr)
* Log a warning level message.
|
void |
warn(String msg,
org.osgi.framework.ServiceReference sr,
Throwable e)
* Log a warning level message.
|
void |
warn(String msg,
Throwable e)
* Log a warning level message.
|
public LogRef(org.osgi.framework.BundleContext bc, boolean out)
bc
- the bundle context of the bundle that this log ref * instance
belongs too. *out
- If true print messages on System.out
when *
there is no log service.public LogRef(org.osgi.framework.BundleContext bc)
* If the system property org.knopflerfish.log.out equals * "true", system.out will be used as fallback if no log service * is found. *
*bc
- the bundle context of the bundle that this log ref * instance
belongs too.public void serviceChanged(org.osgi.framework.ServiceEvent evt)
serviceChanged
in interface org.osgi.framework.ServiceListener
evt
- Service eventpublic void close()
protected void doLog(String msg, int level, org.osgi.framework.ServiceReference sr, Throwable e)
msg
- Human readable string describing the condition. *level
- The severity of the message (Should be one of the * four
predefined severities). *sr
- The ServiceReference
of the service * that this
message is associated with. *e
- The exception that reflects the condition.public int getLogLevel()
public boolean doDebug()
true
if messages with severity LOG_DEBUG * or
higher are included in the log, otherwise false
.public boolean doWarn()
true
if messages with severity LOG_WARNING * or
higher are included in the log, otherwise false
.public boolean doInfo()
true
if messages with severity LOG_INFO * or
higher are included in the log, otherwise false
.public boolean doError()
true
if messages with severity LOG_ERROR * or
higher are included in the log, otherwise false
.public void debug(String msg)
msg
- Log message.public void debug(String msg, org.osgi.framework.ServiceReference sr)
msg
- Log message *sr
- The ServiceReference
of the service * that this
message is associated with.public void debug(String msg, Throwable e)
msg
- Log message *e
- The exception that reflects the condition.public void debug(String msg, org.osgi.framework.ServiceReference sr, Throwable e)
msg
- Log message *sr
- The ServiceReference
of the service * that this
message is associated with. *e
- The exception that reflects the condition.public void info(String msg)
msg
- Log messagepublic void info(String msg, org.osgi.framework.ServiceReference sr)
msg
- Log message *sr
- The ServiceReference
of the service * that this
message is associated with.public void info(String msg, Throwable e)
msg
- Log message *e
- The exception that reflects the condition.public void info(String msg, org.osgi.framework.ServiceReference sr, Throwable e)
msg
- Log message *sr
- The ServiceReference
of the service * that this
message is associated with. *e
- The exception that reflects the condition.public void warn(String msg)
msg
- Log messagepublic void warn(String msg, org.osgi.framework.ServiceReference sr)
msg
- Log message *sr
- The ServiceReference
of the service * that this
message is associated with.public void warn(String msg, Throwable e)
msg
- Log message *e
- The exception that reflects the condition.public void warn(String msg, org.osgi.framework.ServiceReference sr, Throwable e)
msg
- Log message *sr
- The ServiceReference
of the service * that this
message is associated with. *e
- The exception that reflects the condition.public void error(String msg)
msg
- Log messagepublic void error(String msg, org.osgi.framework.ServiceReference sr)
msg
- Log message *sr
- The ServiceReference
of the service * that this
message is associated with.public void error(String msg, Throwable e)
msg
- Log message *e
- The exception that reflects the condition.public void error(String msg, org.osgi.framework.ServiceReference sr, Throwable e)
msg
- Log message *sr
- The ServiceReference
of the service * that this
message is associated with. *e
- The exception that reflects the condition.public void log(int level, String message)
log
in interface LogService
level
- The severity of the message. (Should be one of the * four
predefined severities.) *message
- Human readable string describing the condition.LogService.LOG_ERROR
,
LogService.LOG_WARNING
,
LogService.LOG_INFO
,
LogService.LOG_DEBUG
public void log(int level, String message, Throwable exception)
log
in interface LogService
level
- The severity of the message. (Should be one of the * four
predefined severities.) *message
- Human readable string describing the condition. *exception
- The exception that reflects the condition.LogService.LOG_ERROR
,
LogService.LOG_WARNING
,
LogService.LOG_INFO
,
LogService.LOG_DEBUG
public void log(org.osgi.framework.ServiceReference sr, int level, String message)
log
in interface LogService
sr
- The ServiceReference
of the service that * this
message is associated with. *level
- The severity of the message. (Should be one of the * four
predefined severities.) *message
- Human readable string describing the condition.LogService.LOG_ERROR
,
LogService.LOG_WARNING
,
LogService.LOG_INFO
,
LogService.LOG_DEBUG
public void log(org.osgi.framework.ServiceReference sr, int level, String message, Throwable exception)
log
in interface LogService
sr
- The ServiceReference
of the service that * this
message is associated with. *level
- The severity of the message. (Should be one of the * four
predefined severities.) *message
- Human readable string describing the condition. *exception
- The exception that reflects the condition.LogService.LOG_ERROR
,
LogService.LOG_WARNING
,
LogService.LOG_INFO
,
LogService.LOG_DEBUG
Copyright © 2013. All Rights Reserved.