use of org.osgi.service.log.LogService in project felix by apache.
the class Logger method log.
private void log(int level, String message) {
ServiceReference lsn = bc.getServiceReference(LogService.class.getName());
if (lsn != null) {
LogService ls = (LogService) bc.getService(lsn);
ls.log(level, message);
} else {
System.out.println("ERROR : Logger.start : No service " + LogService.class.getName() + " is present");
}
}
use of org.osgi.service.log.LogService in project felix by apache.
the class Log method log.
/**
* Log the message with the given level and throwable.
* @param level The log level
* @param message The message
* @param t The exception
*/
public void log(final int level, final String message, final Throwable t) {
// log using the LogService if available
@SuppressWarnings("rawtypes") final ServiceTracker tracker = this.logTracker;
final Object log = tracker == null ? null : tracker.getService();
if (log != null) {
((LogService) log).log(serviceReference, level, message, t);
return;
}
// Otherwise only log if more serious than the configured level
if (isLogEnabled(level)) {
String code;
switch(level) {
case LogService.LOG_INFO:
code = "*INFO *";
break;
case LogService.LOG_WARNING:
code = "*WARN *";
break;
case LogService.LOG_ERROR:
code = "*ERROR*";
break;
case LogService.LOG_DEBUG:
default:
code = "*DEBUG*";
}
System.err.println(code + " " + message);
if (t != null) {
t.printStackTrace(System.err);
}
}
}
use of org.osgi.service.log.LogService in project felix by apache.
the class ConfigurationManagerTest method testLogWithLogService.
// this test always expects output since when using a LogService, the log
// level property is ignored
@Test
public void testLogWithLogService() throws IOException {
LogService logService = new MockLogService();
ConfigurationManager configMgr = createConfigurationManagerAndLog(logService);
setLogLevel(LogService.LOG_WARNING);
assertLog(configMgr, LogService.LOG_DEBUG, "Debug Test Message", null);
assertLog(configMgr, LogService.LOG_INFO, "Info Test Message", null);
assertLog(configMgr, LogService.LOG_WARNING, "Warning Test Message", null);
assertLog(configMgr, LogService.LOG_ERROR, "Error Test Message", null);
setLogLevel(LogService.LOG_ERROR);
assertLog(configMgr, LogService.LOG_DEBUG, "Debug Test Message", null);
assertLog(configMgr, LogService.LOG_INFO, "Info Test Message", null);
assertLog(configMgr, LogService.LOG_WARNING, "Warning Test Message", null);
assertLog(configMgr, LogService.LOG_ERROR, "Error Test Message", null);
setLogLevel(LogService.LOG_ERROR - 1);
assertLog(configMgr, LogService.LOG_DEBUG, "Debug Test Message", null);
assertLog(configMgr, LogService.LOG_INFO, "Info Test Message", null);
assertLog(configMgr, LogService.LOG_WARNING, "Warning Test Message", null);
assertLog(configMgr, LogService.LOG_ERROR, "Error Test Message", null);
setLogLevel(Integer.MIN_VALUE);
assertLog(configMgr, LogService.LOG_DEBUG, "Debug Test Message", null);
assertLog(configMgr, LogService.LOG_INFO, "Info Test Message", null);
assertLog(configMgr, LogService.LOG_WARNING, "Warning Test Message", null);
assertLog(configMgr, LogService.LOG_ERROR, "Error Test Message", null);
setLogLevel(LogService.LOG_INFO);
assertLog(configMgr, LogService.LOG_DEBUG, "Debug Test Message", null);
assertLog(configMgr, LogService.LOG_INFO, "Info Test Message", null);
assertLog(configMgr, LogService.LOG_WARNING, "Warning Test Message", null);
assertLog(configMgr, LogService.LOG_ERROR, "Error Test Message", null);
setLogLevel(LogService.LOG_DEBUG);
assertLog(configMgr, LogService.LOG_DEBUG, "Debug Test Message", null);
assertLog(configMgr, LogService.LOG_INFO, "Info Test Message", null);
assertLog(configMgr, LogService.LOG_WARNING, "Warning Test Message", null);
assertLog(configMgr, LogService.LOG_ERROR, "Error Test Message", null);
setLogLevel(Integer.MAX_VALUE);
assertLog(configMgr, LogService.LOG_DEBUG, "Debug Test Message", null);
assertLog(configMgr, LogService.LOG_INFO, "Info Test Message", null);
assertLog(configMgr, LogService.LOG_WARNING, "Warning Test Message", null);
assertLog(configMgr, LogService.LOG_ERROR, "Error Test Message", null);
}
use of org.osgi.service.log.LogService in project felix by apache.
the class Logger method dispatch.
/**
* Internal log method.
*
* @param level the level of the message.
* @param msg the message to log
* @param exception the exception attached to the message
*/
private void dispatch(int level, String msg, Throwable exception) {
LogService log = null;
ServiceReference ref = null;
try {
// Security Check
if (SecurityHelper.hasPermissionToGetService(LogService.class.getName(), m_context)) {
ref = m_context.getServiceReference(LogService.class.getName());
} else {
Extender.getIPOJOBundleContext().getServiceReference(LogService.class.getName());
}
if (ref != null) {
log = (LogService) m_context.getService(ref);
}
} catch (IllegalStateException e) {
// Handle the case where the iPOJO bundle is stopping, or the log service already ran away.
}
if (log == null) {
log = m_defaultLogger;
}
String name = m_name;
if (name == null) {
name = "";
}
String message = String.format("%s %s : %s", getLogHeaderForLevel(level), name, msg);
switch(level) {
case DEBUG:
log.log(LogService.LOG_DEBUG, message, exception);
break;
case INFO:
log.log(LogService.LOG_INFO, message, exception);
break;
case WARNING:
log.log(LogService.LOG_WARNING, message, exception);
break;
case ERROR:
log.log(LogService.LOG_ERROR, message, exception);
break;
default:
log.log(LogService.LOG_INFO, message, exception);
break;
}
if (ref != null) {
m_context.ungetService(ref);
}
}
use of org.osgi.service.log.LogService in project narayana by jbosstm.
the class Activator method warn.
protected void warn(String message, Throwable t) {
ServiceReference<LogService> ref = bundleContext.getServiceReference(LogService.class);
if (ref != null) {
LogService svc = bundleContext.getService(ref);
svc.log(LogService.LOG_WARNING, message, t);
bundleContext.ungetService(ref);
}
}
Aggregations