View Javadoc

1   package com.atlassian.plugin.osgi.container.felix;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.felix.framework.Logger;
5   import org.apache.felix.framework.util.FelixConstants;
6   import org.osgi.framework.BundleException;
7   
8   /**
9    * Bridges Felix logging messages with the Commons Logging
10   */
11  public class FelixLoggerBridge extends Logger {
12      private final Log log;
13  
14      public FelixLoggerBridge(Log log) {
15          this.log = log;
16          setLogLevel(
17                  log.isDebugEnabled() ? Logger.LOG_DEBUG :
18                  log.isInfoEnabled() ? Logger.LOG_WARNING :
19                  Logger.LOG_ERROR);
20      }
21  
22      protected void doLog(org.osgi.framework.ServiceReference serviceReference, int level, java.lang.String message, java.lang.Throwable throwable) {
23          if (serviceReference != null)
24              message = "Service " + serviceReference + ": " + message;
25  
26          switch (level) {
27              case LOG_DEBUG:
28                  log.debug(message);
29                  break;
30              case LOG_ERROR:
31                  if (throwable != null) {
32                      if ((throwable instanceof BundleException) &&
33                              (((BundleException) throwable).getNestedException() != null)) {
34                          throwable = ((BundleException) throwable).getNestedException();
35                      }
36                      log.error(message, throwable);
37                  } else
38                      log.error(message);
39                  break;
40              case LOG_INFO:
41                  log.info(message);
42                  break;
43              case LOG_WARNING:
44                  log.info(message);
45                  break;
46              default:
47                  log.debug("UNKNOWN[" + level + "]: " + message);
48          }
49      }
50  }