Search in sources :

Example 1 with LogEntry

use of org.osgi.service.log.LogEntry in project sling by apache.

the class LogSupport method serviceChanged.

// ---------- ServiceListener ----------------------------------------------
/**
     * Listens for Service events and logs the respective events according to
     * the Log Service specification.
     */
public void serviceChanged(ServiceEvent event) {
    int level = LogService.LOG_INFO;
    String message;
    switch(event.getType()) {
        case ServiceEvent.REGISTERED:
            message = "ServiceEvent REGISTERED";
            break;
        case ServiceEvent.MODIFIED:
            message = "ServiceEvent MODIFIED";
            level = LogService.LOG_DEBUG;
            break;
        case ServiceEvent.UNREGISTERING:
            message = "ServiceEvent UNREGISTERING";
            break;
        default:
            message = "ServiceEvent " + event.getType();
    }
    LogEntry entry = new LogEntryImpl(event.getServiceReference().getBundle(), event.getServiceReference(), level, message, null);
    fireLogEvent(entry);
}
Also used : LogEntry(org.osgi.service.log.LogEntry)

Example 2 with LogEntry

use of org.osgi.service.log.LogEntry in project sling by apache.

the class LogSupportTest method testWarningLogger.

@Test
public void testWarningLogger() throws Exception {
    Exception e = new Exception();
    LogEntry le = new LogEntryImpl(bundle, null, LogService.LOG_WARNING, "my-warning-message", e);
    logSupport.fireLogEvent(le);
    Mockito.verify(testLogger).warn("my-warning-message (java.lang.Exception)", e);
}
Also used : BundleException(org.osgi.framework.BundleException) LogEntry(org.osgi.service.log.LogEntry) Test(org.junit.Test)

Example 3 with LogEntry

use of org.osgi.service.log.LogEntry in project sling by apache.

the class LogSupportTest method testEarlyExit.

@Test
public void testEarlyExit() throws Exception {
    ServiceReference sr = Mockito.mock(ServiceReference.class);
    LogEntry le = new LogEntryImpl(bundle, sr, LogService.LOG_DEBUG, "test", null);
    logSupport.fireLogEvent(le);
    // The log message is on DEBUG level while the logger is set to INFO level
    // we don't want the actual log.info() call to be made, neither do we want
    // any preparatory work on the log message being made (which involves
    // inspecting the service reference).
    Mockito.verify(testLogger).isTraceEnabled();
    Mockito.verify(testLogger).isDebugEnabled();
    Mockito.verify(testLogger).isInfoEnabled();
    Mockito.verifyNoMoreInteractions(testLogger);
    Mockito.verifyZeroInteractions(sr);
}
Also used : LogEntry(org.osgi.service.log.LogEntry) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with LogEntry

use of org.osgi.service.log.LogEntry in project sling by apache.

the class LogSupportTest method testErrorLogger.

@Test
public void testErrorLogger() throws Exception {
    Exception e = new Exception();
    LogEntry le = new LogEntryImpl(bundle, null, LogService.LOG_ERROR, "my-error-msg", e);
    logSupport.fireLogEvent(le);
    Mockito.verify(testLogger).error("my-error-msg (java.lang.Exception)", e);
}
Also used : BundleException(org.osgi.framework.BundleException) LogEntry(org.osgi.service.log.LogEntry) Test(org.junit.Test)

Example 5 with LogEntry

use of org.osgi.service.log.LogEntry in project felix by apache.

the class Basic method log.

@Descriptor("display some matching log entries")
public void log(@Descriptor("maximum number of entries") int maxEntries, @Descriptor("minimum log level [ debug | info | warn | error ]") String logLevel) {
    // Keep track of service references.
    List<ServiceReference<?>> refs = new ArrayList<ServiceReference<?>>();
    // Get start level service.
    LogReaderService lrs = Util.getService(m_bc, LogReaderService.class, refs);
    if (lrs == null) {
        System.out.println("Log reader service is unavailable.");
    } else {
        @SuppressWarnings("unchecked") Enumeration<LogEntry> entries = lrs.getLog();
        int minLevel = logLevelAsInt(logLevel);
        int index = 0;
        while (entries.hasMoreElements() && (maxEntries < 0 || index < maxEntries)) {
            LogEntry entry = (LogEntry) entries.nextElement();
            if (entry.getLevel() <= minLevel) {
                display(entry);
                index++;
            }
        }
        Util.ungetServices(m_bc, refs);
    }
}
Also used : ArrayList(java.util.ArrayList) LogReaderService(org.osgi.service.log.LogReaderService) LogEntry(org.osgi.service.log.LogEntry) ServiceReference(org.osgi.framework.ServiceReference) Descriptor(org.apache.felix.service.command.Descriptor)

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