Search in sources :

Example 21 with LogEntry

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());
}
Also used : LogEntry(org.osgi.service.log.LogEntry)

Example 22 with LogEntry

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());
}
Also used : LogReaderService(org.osgi.service.log.LogReaderService) TestDriver(com.yahoo.jdisc.test.TestDriver) LogEntry(org.osgi.service.log.LogEntry) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 23 with LogEntry

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);
}
Also used : Pattern(java.util.regex.Pattern) Formatter(java.util.Formatter) LogEntry(org.osgi.service.log.LogEntry)

Example 24 with LogEntry

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);
}
Also used : LogEntry(org.osgi.service.log.LogEntry)

Example 25 with LogEntry

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);
}
Also used : BundleException(org.osgi.framework.BundleException) LogEntry(org.osgi.service.log.LogEntry)

Aggregations

LogEntry (org.osgi.service.log.LogEntry)40 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)6 LogReaderService (org.osgi.service.log.LogReaderService)5 BundleException (org.osgi.framework.BundleException)4 Enumeration (java.util.Enumeration)3 ServiceReference (org.osgi.framework.ServiceReference)3 LogListener (org.osgi.service.log.LogListener)3 SynchronousLogListener (org.eclipse.equinox.log.SynchronousLogListener)2 BundleContext (org.osgi.framework.BundleContext)2 TestDriver (com.yahoo.jdisc.test.TestDriver)1 File (java.io.File)1 Formatter (java.util.Formatter)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Vector (java.util.Vector)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1