use of org.osgi.service.log.LogEntry in project vespa by vespa-engine.
the class OsgiLogServiceIntegrationTest method assertLogContainsEntry.
private static void assertLogContainsEntry(String expectedMessage, List<LogEntry> logEntries, long expectedTimeGE) {
LogEntry entry = logEntries.stream().filter(e -> e.getMessage().equals(expectedMessage)).findFirst().orElseThrow(() -> new AssertionError("Could not find log entry with messsage: " + expectedMessage));
assertNull(entry.getBundle());
assertNotNull(entry.getServiceReference());
assertEquals(OsgiLogHandler.toServiceLevel(Level.INFO), entry.getLevel());
assertNull(entry.getException());
assertTrue(expectedTimeGE <= entry.getTime());
}
use of org.osgi.service.log.LogEntry in project vespa by vespa-engine.
the class OsgiLogServiceIntegrationTest method requireThatAllSupportedLogFrameworksAreConfigured.
@Test
@SuppressWarnings("unchecked")
public void requireThatAllSupportedLogFrameworksAreConfigured() throws Exception {
// need to explicitly set log level of root logger since integration suite now provides a logger config file,
// which disables that setLevel() call of the OsgiLogManager.
Logger.getLogger("").setLevel(Level.INFO);
long now = System.currentTimeMillis();
TestDriver driver = TestDriver.newApplicationBundleInstance("app-h-log.jar", false);
BundleContext ctx = driver.osgiFramework().bundleContext();
ServiceReference<?> ref = ctx.getServiceReference(LogReaderService.class.getName());
LogReaderService reader = (LogReaderService) ctx.getService(ref);
ArrayList<LogEntry> logEntries = Collections.list(reader.getLog());
assertTrue(logEntries.size() >= 4);
assertLogContainsEntry("[jdk14] hello world", logEntries, now);
assertLogContainsEntry("[slf4j] hello world", logEntries, now);
assertLogContainsEntry("[log4j] hello world", logEntries, now);
assertLogContainsEntry("[jcl] hello world", logEntries, now);
assertTrue(driver.close());
}
use of org.osgi.service.log.LogEntry in project bnd by bndtools.
the class TestingLog method log.
public synchronized void log(final ServiceReference sr, final int level, final String message, final Throwable exception) {
if (exception != null && stacktrace)
exception.printStackTrace();
if (level < this.level)
return;
for (Pattern p : filters) {
if (p.matcher(message).find())
return;
}
final long now = System.currentTimeMillis();
LogEntry entry = new LogEntry() {
public long getTime() {
return now;
}
public ServiceReference getServiceReference() {
return sr;
}
public String getMessage() {
return message;
}
public int getLevel() {
return level;
}
public Throwable getException() {
return exception;
}
public Bundle getBundle() {
return null;
}
public String toString() {
StringBuilder sb = new StringBuilder();
try (Formatter f = new Formatter(sb)) {
f.format("%6s %-4s %s %s", (now - start + 500) / 1000, (sr == null ? "" : sr.getProperty("service.id")), message, (exception == null ? "" : exception));
return sb.toString();
}
}
};
entries.add(entry);
if (direct)
System.out.println(entry);
}
use of org.osgi.service.log.LogEntry in project sling by apache.
the class LogSupport method bundleChanged.
// ---------- BundleListener -----------------------------------------------
/**
* Listens for Bundle events and logs the respective events according to the
* Log Service specification. In addition, all LogListener instances
* registered for stopped bundles are removed by this method.
*/
public void bundleChanged(BundleEvent event) {
String message;
switch(event.getType()) {
case BundleEvent.INSTALLED:
message = "BundleEvent INSTALLED";
break;
case BundleEvent.RESOLVED:
message = "BundleEvent RESOLVED";
break;
case BundleEvent.STARTING:
message = "BundleEvent STARTING";
break;
case BundleEvent.STARTED:
message = "BundleEvent STARTED";
break;
case BundleEvent.STOPPING:
message = "BundleEvent STOPPING";
break;
case BundleEvent.STOPPED:
// this is special, as we have to fix the listener list for
// stopped bundles
removeLogListeners(event.getBundle());
message = "BundleEvent STOPPED";
break;
case BundleEvent.UNRESOLVED:
message = "BundleEvent UNRESOLVED";
break;
case BundleEvent.UPDATED:
message = "BundleEvent UPDATED";
break;
case BundleEvent.UNINSTALLED:
// remove any cached logger for the uninstalled bundle
ungetLogger(event.getBundle());
message = "BundleEvent UNINSTALLED";
break;
default:
message = "BundleEvent " + event.getType();
}
LogEntry entry = new LogEntryImpl(event.getBundle(), null, LogService.LOG_INFO, message, null);
fireLogEvent(entry);
}
use of org.osgi.service.log.LogEntry in project sling by apache.
the class LogSupport method frameworkEvent.
// ---------- FrameworkListener --------------------------------------------
/**
* Listens for Framework events and logs the respective events according to
* the Log Service specification.
* <p>
* In the case of a Framework ERROR which is a ClassNotFoundException for an
* unresolved bundle, the message is logged at INFO level instead of ERROR
* level as prescribed by the spec. This is because such a situation should
* not really result in a Framework ERROR but the Apache Felix framework has
* no means of controlling this at the moment (framework 1.0.4 release).
*/
public void frameworkEvent(FrameworkEvent event) {
int level = LogService.LOG_INFO;
String message;
Throwable exception = event.getThrowable();
switch(event.getType()) {
case FrameworkEvent.STARTED:
message = "FrameworkEvent STARTED";
break;
case FrameworkEvent.ERROR:
message = "FrameworkEvent ERROR";
// resolve
if (exception instanceof BundleException) {
StackTraceElement[] ste = exception.getStackTrace();
if (ste != null && ste.length > 0 && "loadBundleClass".equals(ste[0].getMethodName())) {
message += ": Class " + exception.getMessage() + " not found";
if (event.getBundle() != null) {
message += " in bundle " + event.getBundle().getSymbolicName() + " (" + event.getBundle().getBundleId() + ")";
}
level = LogService.LOG_INFO;
// don't care for a stack trace here
exception = null;
break;
}
}
level = LogService.LOG_ERROR;
break;
case FrameworkEvent.PACKAGES_REFRESHED:
message = "FrameworkEvent PACKAGES REFRESHED";
break;
case FrameworkEvent.STARTLEVEL_CHANGED:
message = "FrameworkEvent STARTLEVEL CHANGED to " + this.startLevelService.getStartLevel();
break;
case FrameworkEvent.WARNING:
message = "FrameworkEvent WARNING";
break;
case FrameworkEvent.INFO:
message = "FrameworkEvent INFO";
break;
default:
message = "FrameworkEvent " + event.getType();
}
final LogEntry entry = new LogEntryImpl(event.getBundle(), null, level, message, exception);
fireLogEvent(entry);
}
Aggregations