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   import java.util.List;
9   import java.util.Arrays;
10  
11  /**
12   * Bridges Felix logging messages with the Commons Logging
13   */
14  public class FelixLoggerBridge extends Logger {
15      private final Log log;
16  
17      private static final List<String> messagesToIgnore = Arrays.asList(
18              "BeanInfo'. Please",
19              "BeanInfo' was not found",
20              "sun.beans.editors.",
21              "add an import for 'org.springframework.osgi.service.",
22              "Class 'org.springframework.util.Assert'",
23              "Class '[Lorg.springframework.osgi.service",
24              "Class 'org.springframework.core.InfrastructureProxy'",
25              "Class 'org.springframework.aop.SpringProxy'",
26              "Class 'org.springframework.aop.IntroductionInfo'",
27              "Class 'org.apache.commons.logging.impl.Log4JLogger'"
28      );
29  
30      public FelixLoggerBridge(Log log) {
31          this.log = log;
32          setLogLevel(
33                  log.isDebugEnabled() ? Logger.LOG_DEBUG :
34                  log.isInfoEnabled() ? Logger.LOG_WARNING :
35                  Logger.LOG_ERROR);
36      }
37  
38      protected void doLog(org.osgi.framework.ServiceReference serviceReference, int level, java.lang.String message, java.lang.Throwable throwable) {
39          if (serviceReference != null)
40              message = "Service " + serviceReference + ": " + message;
41  
42          switch (level) {
43              case LOG_DEBUG:
44                  log.debug(message);
45                  break;
46              case LOG_ERROR:
47                  if (throwable != null) {
48                      if ((throwable instanceof BundleException) &&
49                              (((BundleException) throwable).getNestedException() != null)) {
50                          throwable = ((BundleException) throwable).getNestedException();
51                      }
52                      log.error(message, throwable);
53                  } else
54                      log.error(message);
55                  break;
56              case LOG_INFO:
57                  logInfoUnlessLame(message);
58                  break;
59              case LOG_WARNING:
60                  logInfoUnlessLame(message);
61                  break;
62              default:
63                  log.debug("UNKNOWN[" + level + "]: " + message);
64          }
65      }
66  
67      protected void logInfoUnlessLame(String message)
68      {
69          if (message != null)
70          {
71              // I'm really, really sick of these stupid messages
72              for (String dumbBit : messagesToIgnore)
73                  if (message.contains(dumbBit))
74                      return;
75          }
76          log.info(message);
77      }
78  }