3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.logging.bridge.internal;
12 import org.osgi.service.log.LogEntry;
13 import java.util.Enumeration;
14 import org.osgi.framework.BundleContext;
15 import org.osgi.framework.BundleActivator;
16 import org.osgi.framework.ServiceRegistration;
17 import org.osgi.framework.ServiceReference;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.slf4j.ILoggerFactory;
21 import org.osgi.service.log.LogReaderService;
23 public class Activator implements BundleActivator {
24 private LogListenerImpl listener = null;
25 private Logger log = null;
28 public void start(BundleContext context) {
29 // Lets trigger the resolution of the slf4j logger factory
30 ILoggerFactory f = LoggerFactory.getILoggerFactory();
32 // Now retrieve a logger for the bridge
34 .getLogger("org.opendaylight.controller.logging.bridge.OSGI2SLF4J");
36 if (this.log != null) {
37 this.listener = new LogListenerImpl(log);
39 ServiceReference service = null;
40 service = context.getServiceReference(LogReaderService.class
42 if (service != null) {
43 LogReaderService reader = (LogReaderService) context
46 this.log.error("Cannot register the LogListener because "
47 + "cannot retrive LogReaderService");
49 reader.addLogListener(this.listener);
50 // Now lets walk all the exiting messages
51 Enumeration<LogEntry> entries = reader.getLog();
52 if (entries != null) {
53 while (entries.hasMoreElements()) {
54 LogEntry entry = (LogEntry) entries.nextElement();
55 this.listener.logged(entry);
60 * Install the default exception handler so that the uncaught
61 * exceptions are handled by our customized handler. This new
62 * handler will display the exceptions to OSGI console as well
65 Thread.setDefaultUncaughtExceptionHandler(new org.opendaylight.
66 controller.logging.bridge.internal.UncaughtExceptionHandler());
68 this.log.error("Cannot register the LogListener because "
69 + "cannot retrive LogReaderService");
73 .println("Could not initialize the logging bridge subsytem");
78 public void stop(BundleContext context) {
79 ServiceReference service = null;
80 service = context.getServiceReference(LogReaderService.class.getName());
81 if (service != null) {
82 LogReaderService reader = (LogReaderService) service;
83 reader.removeLogListener(this.listener);