Logging related enhancements.
[controller.git] / opendaylight / logging / bridge / src / main / java / org / opendaylight / controller / logging / bridge / internal / Activator.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.logging.bridge.internal;
11
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;
22
23 public class Activator implements BundleActivator {
24     private LogListenerImpl listener = null;
25     private Logger log = null;
26
27     @Override
28     public void start(BundleContext context) {
29         // Lets trigger the resolution of the slf4j logger factory
30         ILoggerFactory f = LoggerFactory.getILoggerFactory();
31
32         // Now retrieve a logger for the bridge
33         log = f
34                 .getLogger("org.opendaylight.controller.logging.bridge.OSGI2SLF4J");
35
36         if (this.log != null) {
37             this.listener = new LogListenerImpl(log);
38
39             ServiceReference service = null;
40             service = context.getServiceReference(LogReaderService.class
41                     .getName());
42             if (service != null) {
43                 LogReaderService reader = (LogReaderService) context
44                         .getService(service);
45                 if (reader == null) {
46                     this.log.error("Cannot register the LogListener because "
47                             + "cannot retrieve LogReaderService");
48                 }
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);
56                     }
57                 }
58                 
59                 /*
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
63                  * as log to file.
64                  */
65                 Thread.setDefaultUncaughtExceptionHandler(new org.opendaylight.
66                         controller.logging.bridge.internal.UncaughtExceptionHandler());
67             } else {
68                 this.log.error("Cannot register the LogListener because "
69                         + "cannot retrieve LogReaderService");
70             }
71         } else {
72             System.err
73                     .println("Could not initialize the logging bridge subsytem");
74         }
75     }
76
77     @Override
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);
84         }
85
86         this.listener = null;
87         this.log = null;
88     }
89 }