You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by sa...@apache.org on 2010/10/14 20:24:11 UTC

svn commit: r1022638 - in /felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl: Configuration.java util/LogWrapper.java

Author: sahoo
Date: Thu Oct 14 18:24:11 2010
New Revision: 1022638

URL: http://svn.apache.org/viewvc?rev=1022638&view=rev
Log:
FELIX-2655: Make log level configurable

Modified:
    felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/Configuration.java
    felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/util/LogWrapper.java

Modified: felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/Configuration.java
URL: http://svn.apache.org/viewvc/felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/Configuration.java?rev=1022638&r1=1022637&r2=1022638&view=diff
==============================================================================
--- felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/Configuration.java (original)
+++ felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/Configuration.java Thu Oct 14 18:24:11 2010
@@ -107,6 +107,7 @@ public class Configuration
     static final String PROP_TIMEOUT = "org.apache.felix.eventadmin.Timeout";
     static final String PROP_REQUIRE_TOPIC = "org.apache.felix.eventadmin.RequireTopic";
     static final String PROP_IGNORE_TIMEOUT = "org.apache.felix.eventadmin.IgnoreTimeout";
+    static final String PROP_LOG_LEVEL = "org.apache.felix.eventadmin.LogLevel";
 
     /** The bundle context. */
     private final BundleContext m_bundleContext;
@@ -121,6 +122,8 @@ public class Configuration
 
     private String[] m_ignoreTimeout;
 
+    private int m_logLevel;
+
     // The thread pool used - this is a member because we need to close it on stop
     private volatile DefaultThreadPool m_sync_pool;
 
@@ -250,6 +253,10 @@ public class Configuration
                     m_ignoreTimeout[i] = st.nextToken();
                 }
             }
+            m_logLevel = getIntProperty(PROP_LOG_LEVEL,
+                    m_bundleContext.getProperty(PROP_LOG_LEVEL),
+                    LogWrapper.LOG_WARNING, // default log level is WARNING
+                    LogWrapper.LOG_ERROR);
         }
         else
         {
@@ -272,6 +279,10 @@ public class Configuration
                 LogWrapper.getLogger().log(LogWrapper.LOG_WARNING,
                         "Value for property: " + PROP_IGNORE_TIMEOUT + " is neither a string nor a string array - Using default");
             }
+            m_logLevel = getIntProperty(PROP_LOG_LEVEL,
+                    config.get(PROP_LOG_LEVEL),
+                    LogWrapper.LOG_WARNING, // default log level is WARNING
+                    LogWrapper.LOG_ERROR);
         }
         // a timeout less or equals to 100 means : disable timeout
         if ( m_timeout <= 100 )
@@ -282,6 +293,9 @@ public class Configuration
 
     private void startOrUpdate()
     {
+        LogWrapper.getLogger().setLogLevel(m_logLevel);
+        LogWrapper.getLogger().log(LogWrapper.LOG_DEBUG,
+                PROP_LOG_LEVEL + "=" + m_logLevel);
         LogWrapper.getLogger().log(LogWrapper.LOG_DEBUG,
                 PROP_CACHE_SIZE + "=" + m_cacheSize);
         LogWrapper.getLogger().log(LogWrapper.LOG_DEBUG,

Modified: felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/util/LogWrapper.java
URL: http://svn.apache.org/viewvc/felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/util/LogWrapper.java?rev=1022638&r1=1022637&r2=1022638&view=diff
==============================================================================
--- felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/util/LogWrapper.java (original)
+++ felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/util/LogWrapper.java Thu Oct 14 18:24:11 2010
@@ -43,6 +43,10 @@ import org.osgi.framework.ServiceReferen
  * use it if present. Additionally, all log methods prefix the log message with
  * <tt>EventAdmin: </tt>.
  *
+ * There is one difference in behavior from the standard OSGi LogService.
+ * This logger has a {@link #m_logLevel} property which decides what messages
+ * get logged.
+ *
  * @see org.osgi.service.log.LogService
  *
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -88,6 +92,15 @@ public class LogWrapper
     private BundleContext m_context;
 
     private ServiceListener m_logServiceListener;
+
+    /**
+     * Current log level. Message with log level less than or equal to
+     * current log level will be logged.
+     * The default value is {@link #LOG_WARNING}
+     *
+     * @see #setLogLevel(int)
+     */
+    private int m_logLevel = LOG_WARNING;
     /*
      * A thread save variant of the double checked locking singleton.
      */
@@ -230,6 +243,11 @@ public class LogWrapper
         // The method will remove any unregistered service reference as well.
         synchronized(m_loggerRefs)
         {
+            if (level > m_logLevel)
+            {
+                return; // don't log
+            }
+
             final String logMsg = "EventAdmin: " + msg;
 
             if (!m_loggerRefs.isEmpty())
@@ -277,6 +295,11 @@ public class LogWrapper
         // The method will remove any unregistered service reference as well.
         synchronized(m_loggerRefs)
         {
+            if (level > m_logLevel)
+            {
+                return; // don't log
+            }
+
             final String logMsg = "EventAdmin: " + msg;
 
             if (!m_loggerRefs.isEmpty())
@@ -324,6 +347,11 @@ public class LogWrapper
         // The method will remove any unregistered service reference as well.
         synchronized(m_loggerRefs)
         {
+            if (level > m_logLevel)
+            {
+                return; // don't log
+            }
+
             final String logMsg = "EventAdmin: " + msg;
 
             if (!m_loggerRefs.isEmpty())
@@ -373,6 +401,11 @@ public class LogWrapper
         // The method will remove any unregistered service reference as well.
         synchronized(m_loggerRefs)
         {
+            if (level > m_logLevel)
+            {
+                return; // don't log
+            }
+            
             final String logMsg = "EventAdmin: " + msg;
 
             if (!m_loggerRefs.isEmpty())
@@ -446,4 +479,30 @@ public class LogWrapper
                 System.out.println("UNKNOWN[" + level + "]: " + s);
         }
     }
+
+    /**
+     * Change the current log level. Log level decides what messages gets
+     * logged. Any message with a log level higher than the currently set
+     * log level is not logged.
+     *
+     * @param logLevel new log level
+     */
+    public void setLogLevel(int logLevel)
+    {
+        synchronized (m_loggerRefs)
+        {
+            m_logLevel = logLevel;
+        }
+    }
+
+    /**
+     * @return current log level.
+     */
+    public int getLogLevel()
+    {
+        synchronized (m_loggerRefs)
+        {
+            return m_logLevel;
+        }
+    }
 }