You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ch...@apache.org on 2015/07/22 07:03:34 UTC

svn commit: r1692218 - in /sling/trunk/bundles/commons/log/src: main/java/org/apache/sling/commons/log/logback/internal/ test/java/org/apache/sling/commons/log/logback/integration/ test/java/org/apache/sling/commons/log/logback/internal/

Author: chetanm
Date: Wed Jul 22 05:03:34 2015
New Revision: 1692218

URL: http://svn.apache.org/r1692218
Log:
SLING-4840 - Provide an option to disable a set of logger via config

LogConfig now supports resetToDefault mode which is mapped to log level of DEFAULT. If enabled then logger state would be set to default i.e. no explicit log level set, additive to true and no appender explaicitly attached

Modified:
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfig.java
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingLogPanel.java
    sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/integration/ITConfigAdminSupport.java
    sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestLogConfig.java

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfig.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfig.java?rev=1692218&r1=1692217&r2=1692218&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfig.java (original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfig.java Wed Jul 22 05:03:34 2015
@@ -49,8 +49,10 @@ public class LogConfig {
 
     private final boolean isAdditiv;
 
+    private final boolean resetToDefault;
+
     LogConfig(LogWriterProvider logWriterProvider, final String pattern, Set<String> categories, Level logLevel,
-            String logWriterName, final boolean isAdditiv, String configPid, LoggerContext loggerContext) {
+              String logWriterName, final boolean isAdditiv, String configPid, LoggerContext loggerContext, boolean resetToDefault) {
         this.logWriterProvider = logWriterProvider;
         this.configPid = configPid;
         this.pattern = pattern;
@@ -59,6 +61,7 @@ public class LogConfig {
         this.logWriterName = logWriterName;
         this.loggerContext = loggerContext;
         this.isAdditiv = isAdditiv;
+        this.resetToDefault = resetToDefault;
     }
 
     public String getConfigPid() {
@@ -89,6 +92,10 @@ public class LogConfig {
         return logWriterProvider.getLogWriter(getLogWriterName());
     }
 
+    public boolean isResetToDefault() {
+        return resetToDefault;
+    }
+
     public PatternLayout createLayout() {
         // The java.util.MessageFormat pattern to use for formatting log
         // messages with the root logger.
@@ -139,7 +146,7 @@ public class LogConfig {
             + logLevel + ", logWriterName='" + logWriterName + '\'' + '}';
     }
 
-    public static interface LogWriterProvider {
+    public interface LogWriterProvider {
         LogWriter getLogWriter(String writerName);
     }
 

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java?rev=1692218&r1=1692217&r2=1692218&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java (original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java Wed Jul 22 05:03:34 2015
@@ -76,6 +76,8 @@ public class LogConfigManager implements
 
     public static final String LOG_LEVEL_DEFAULT = "INFO";
 
+    public static final String LOG_LEVEL_RESET_TO_DEFAULT = "DEFAULT";
+
     public static final int LOG_FILE_NUMBER_DEFAULT = 5;
 
     public static final String LOG_FILE_SIZE_DEFAULT = "'.'yyyy-MM-dd";
@@ -293,12 +295,17 @@ public class LogConfigManager implements
 
             for (String category : config.getCategories()) {
                 ch.qos.logback.classic.Logger logger = loggerContext.getLogger(category);
-                logger.setLevel(config.getLogLevel());
-                if (appender != null) {
-                    logger.setAdditive(config.isAdditive());
-                    logger.addAppender(appender);
-                    contextUtil.addInfo("Registering appender "+appender.getName()+ "("+appender.getClass()+
-                            ") with logger "+logger.getName());
+                if (config.isResetToDefault()){
+                    logger.setLevel(null);
+                    logger.setAdditive(true); //Reset additivity
+                } else {
+                    logger.setLevel(config.getLogLevel());
+                    if (appender != null) {
+                        logger.setAdditive(config.isAdditive());
+                        logger.addAppender(appender);
+                        contextUtil.addInfo("Registering appender " + appender.getName() + "(" + appender.getClass() +
+                                ") with logger " + logger.getName());
+                    }
                 }
             }
         }
@@ -520,10 +527,15 @@ public class LogConfigManager implements
             if (level == null) {
                 throw new ConfigurationException(LogConfigManager.LOG_LEVEL, "Value required");
             }
-            // TODO: support numeric levels !
-            final Level logLevel = Level.toLevel(level);
-            if (logLevel == null) {
-                throw new ConfigurationException(LogConfigManager.LOG_LEVEL, "Unsupported value: " + level);
+
+            final Level logLevel;
+            final boolean resetToDefault;
+            if (LOG_LEVEL_RESET_TO_DEFAULT.equalsIgnoreCase(level)){
+                resetToDefault = true;
+                logLevel = null;
+            } else {
+                logLevel = Level.toLevel(level);
+                resetToDefault = false;
             }
 
             // verify pattern
@@ -538,7 +550,8 @@ public class LogConfigManager implements
             }
 
             // create or modify existing configuration object
-            final LogConfig newConfig = new LogConfig(this, pattern, categories, logLevel, fileName, additiv, pid, loggerContext);
+            final LogConfig newConfig = new LogConfig(this, pattern, categories, logLevel, fileName, additiv,
+                    pid, loggerContext, resetToDefault);
             LogConfig oldConfig = configByPid.get(pid);
             if (oldConfig != null) {
                 configByCategory.keySet().removeAll(oldConfig.getCategories());
@@ -661,7 +674,7 @@ public class LogConfigManager implements
      * Returns the <code>logFileName</code> argument converted into an absolute
      * path name. If <code>logFileName</code> is already absolute it is returned
      * unmodified. Otherwise it is made absolute by resolving it relative to the
-     * root directory set on this instance by the {@link #setRoot(String)}
+     * root directory set on this instance.
      * method.
      *
      * @throws NullPointerException if <code>logFileName</code> is

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingLogPanel.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingLogPanel.java?rev=1692218&r1=1692217&r2=1692218&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingLogPanel.java (original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingLogPanel.java Wed Jul 22 05:03:34 2015
@@ -87,7 +87,8 @@ public class SlingLogPanel extends HttpS
             Level.INFO.levelStr,
             Level.DEBUG.levelStr,
             Level.TRACE.levelStr,
-            Level.OFF.levelStr
+            Level.OFF.levelStr,
+            LogConfigManager.LOG_LEVEL_RESET_TO_DEFAULT
     };
 
     private static final String PACKAGE_SEPARATOR = ".";
@@ -228,9 +229,9 @@ public class SlingLogPanel extends HttpS
             pw.print( XmlUtil.escapeXml(logConfig.getConfigPid()) );
             pw.println("\">");
             pw.print("<td><span class=\"logLevels\" data-currentloglevel=\"");
-            pw.print(logConfig.getLogLevel().levelStr);
+            pw.print(getLevelStr(logConfig));
             pw.print("\">");
-            pw.print(logConfig.getLogLevel().levelStr);
+            pw.print(getLevelStr(logConfig));
             pw.println("</span></td>");
             pw.print("<td><span class=\"logFile\">");
             pw.print( XmlUtil.escapeXml(getPath(logConfig.getLogWriterName(), rootPath, shortenPaths)));
@@ -682,6 +683,13 @@ public class SlingLogPanel extends HttpS
         logbackManager.getLogConfigManager().internalFailure(msg, e);
     }
 
+    private String getLevelStr(LogConfig logConfig) {
+        if (logConfig.isResetToDefault()){
+            return LogConfigManager.LOG_LEVEL_RESET_TO_DEFAULT;
+        }
+        return logConfig.getLogLevel().levelStr;
+    }
+
     private static String getName(TurboFilter tf) {
         if (tf.getName() != null) {
             return String.format("%s (%s)", tf.getName(), tf.getClass().getName());

Modified: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/integration/ITConfigAdminSupport.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/integration/ITConfigAdminSupport.java?rev=1692218&r1=1692217&r2=1692218&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/integration/ITConfigAdminSupport.java (original)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/integration/ITConfigAdminSupport.java Wed Jul 22 05:03:34 2015
@@ -25,6 +25,7 @@ import java.util.Iterator;
 
 import javax.inject.Inject;
 
+import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.core.Appender;
@@ -39,6 +40,7 @@ import org.osgi.service.cm.Configuration
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -117,6 +119,31 @@ public class ITConfigAdminSupport extend
     }
 
     @Test
+    public void testResetToDefault() throws Exception {
+        ch.qos.logback.classic.Logger lgLog =
+                (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("foo2.bar");
+
+        lgLog.setLevel(Level.TRACE);
+        assertEquals(Level.TRACE, lgLog.getLevel());
+
+        // Set log level to debug for foo2.bar
+        Configuration config = ca.createFactoryConfiguration(FACTORY_PID_CONFIGS, null);
+        Dictionary<String, Object> p = new Hashtable<String, Object>();
+        p.put(LOG_LOGGERS, new String[]{
+                "foo2.bar"
+        });
+        p.put(LOG_LEVEL, "DEFAULT");
+        config.update(p);
+
+        delay();
+
+        Logger slf4jLogger = LoggerFactory.getLogger("foo2.bar");
+        assertFalse(slf4jLogger.isDebugEnabled());
+        assertFalse(slf4jLogger.isTraceEnabled());
+        assertTrue(lgLog.isAdditive());
+    }
+
+    @Test
     public void testChangeGlobalConfig() throws Exception {
         // Set log level to debug for Root logger
         Configuration config = ca.getConfiguration(PID, null);

Modified: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestLogConfig.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestLogConfig.java?rev=1692218&r1=1692217&r2=1692218&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestLogConfig.java (original)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestLogConfig.java Wed Jul 22 05:03:34 2015
@@ -49,7 +49,7 @@ public class TestLogConfig {
 
     private LogConfig createConfig(String pattern) {
         return new LogConfig(new DummyLogWriterProvider(), pattern, Collections.<String> emptySet(), Level.DEBUG,
-            "test", false, null, (LoggerContext) LoggerFactory.getILoggerFactory());
+            "test", false, null, (LoggerContext) LoggerFactory.getILoggerFactory(), false);
     }
 
     private static class DummyLogWriterProvider implements LogConfig.LogWriterProvider {