incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1449656 - in /sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src: main/java/org/apache/sling/commons/log/internal/ main/java/org/apache/sling/commons/log/internal/slf4j/ main/resources/OSGI-INF/metatype/ test/java/org/apache...
Date Mon, 25 Feb 2013 10:51:07 GMT
Author: bdelacretaz
Date: Mon Feb 25 10:51:06 2013
New Revision: 1449656

URL: http://svn.apache.org/r1449656
Log:
slf4j marker support added, see testMarkers in SlingLoggerTest.java

Modified:
    sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/LogManager.java
    sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java
    sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLogger.java
    sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerConfig.java
    sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.properties
    sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.xml
    sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java

Modified: sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/LogManager.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/LogManager.java?rev=1449656&r1=1449655&r2=1449656&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/LogManager.java
(original)
+++ sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/LogManager.java
Mon Feb 25 10:51:06 2013
@@ -49,6 +49,8 @@ public class LogManager {
 
     public static final String LOG_PATTERN_DEFAULT = "{0,date,dd.MM.yyyy HH:mm:ss.SSS} *{4}*
[{2}] {3} {5}";
 
+    public static final String LOG_MARKERS = "org.apache.sling.commons.log.markers";
+   
     public static final String LOG_LOGGERS = "org.apache.sling.commons.log.names";
 
     public static final String LOG_LEVEL_DEFAULT = "INFO";

Modified: sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java?rev=1449656&r1=1449655&r2=1449656&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java
(original)
+++ sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java
Mon Feb 25 10:51:06 2013
@@ -119,7 +119,7 @@ public class LogConfigManager implements
         Set<String> defaultCategories = new HashSet<String>();
         defaultCategories.add(ROOT);
         defaultLoggerConfig = new SlingLoggerConfig(LogManager.PID,
-            LogManager.LOG_PATTERN_DEFAULT, defaultCategories,
+            LogManager.LOG_PATTERN_DEFAULT, defaultCategories, null,
             SlingLoggerLevel.INFO, defaultWriter);
         configByPid.put(LogManager.PID, defaultLoggerConfig);
         configByCategory.put(ROOT, defaultLoggerConfig);
@@ -439,6 +439,7 @@ public class LogConfigManager implements
             String level = (String) configuration.get(LogManager.LOG_LEVEL);
             String file = (String) configuration.get(LogManager.LOG_FILE);
             Set<String> categories = toCategoryList(configuration.get(LogManager.LOG_LOGGERS));
+            Set<String> markers = toStringSet(configuration.get(LogManager.LOG_MARKERS));
 
             // verify categories
             if (categories == null) {
@@ -447,6 +448,7 @@ public class LogConfigManager implements
             }
 
             // verify no other configuration has any of the categories
+            /* TODO - removing, to support markers
             for (String cat : categories) {
                 SlingLoggerConfig cfg = configByCategory.get(cat);
                 if (cfg != null && !pid.equals(cfg.getConfigPid())) {
@@ -455,6 +457,7 @@ public class LogConfigManager implements
                             + " already defined by configuration " + pid);
                 }
             }
+            */
 
             // verify writer
             SlingLoggerWriter writer;
@@ -495,7 +498,7 @@ public class LogConfigManager implements
             if (config == null) {
 
                 // create and store new configuration
-                config = new SlingLoggerConfig(pid, pattern, categories,
+                config = new SlingLoggerConfig(pid, pattern, categories, markers,
                     logLevel, writer);
                 configByPid.put(pid, config);
 
@@ -508,7 +511,7 @@ public class LogConfigManager implements
                 final SlingLoggerWriter oldWriter = config.getLogWriter();
 
                 // reconfigure the configuration
-                config.configure(pattern, categories, logLevel, writer);
+                config.configure(pattern, categories, markers, logLevel, writer);
 
                 if (categories.equals(oldCategories)) {
 
@@ -648,49 +651,56 @@ public class LogConfigManager implements
      *         object or <code>null</code> if the <code>loggers</code>
      *         object itself is <code>null</code>.
      */
-    private Set<String> toCategoryList(Object loggers) {
+    static private Set<String> toCategoryList(Object loggers) {
 
         // quick exit if there is no configuration
         if (loggers == null) {
             return null;
         }
 
-        // prepare set of names (used already in case loggers == ROOT)
-        Set<String> loggerNames = new HashSet<String>();
-
         // in case of the special setting ROOT, return a set of just the
         // root logger name (SLING-529)
         if (loggers == ROOT) {
+            final Set<String> loggerNames = new HashSet<String>();
             loggerNames.add(ROOT);
             return loggerNames;
         }
+        
+        return toStringSet(loggers);
+    }
+    
+    static private Set<String> toStringSet(Object o) {
 
+        final Set<String> result = new HashSet<String>();
+        if(o == null) {
+            return result;
+        }
+        
         // convert the loggers object to an array
-        Object[] loggersArray;
-        if (loggers.getClass().isArray()) {
-            loggersArray = (Object[]) loggers;
-        } else if (loggers instanceof Collection<?>) {
-            loggersArray = ((Collection<?>) loggers).toArray();
+        Object[] array;
+        if (o.getClass().isArray()) {
+            array = (Object[]) o;
+        } else if (o instanceof Collection<?>) {
+            array = ((Collection<?>) o).toArray();
         } else {
-            loggersArray = new Object[] { loggers };
+            array = new Object[] { o };
         }
 
         // conver the array of potentially comma-separated logger names
         // into the set of logger names
-        for (Object loggerObject : loggersArray) {
-            if (loggerObject != null) {
-                String[] splitLoggers = loggerObject.toString().split(",");
-                for (String logger : splitLoggers) {
-                    logger = logger.trim();
-                    if (logger.length() > 0) {
-                        loggerNames.add(logger);
+        for (Object obj : array) {
+            if (obj != null) {
+                String[] split = obj.toString().split(",");
+                for (String s : split) {
+                    s = s.trim();
+                    if (s.length() > 0) {
+                        result.add(s);
                     }
                 }
             }
         }
-
-        // return those names
-        return loggerNames;
+        
+        return result;
     }
 
     /**

Modified: sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLogger.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLogger.java?rev=1449656&r1=1449655&r2=1449656&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLogger.java
(original)
+++ sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLogger.java
Mon Feb 25 10:51:06 2013
@@ -58,7 +58,10 @@ class SlingLogger implements LocationAwa
 
     private void log(Marker marker, String fqcn, SlingLoggerLevel level,
             String msg, Throwable t) {
-        StringWriter writer = new StringWriter();
+        if(!config.markerPasses(marker)) {
+            return;
+        }
+        final StringWriter writer = new StringWriter();
 
         // create the formatted log line; use a local copy because the field
         // may be exchanged while we are trying to use it
@@ -468,9 +471,9 @@ class SlingLogger implements LocationAwa
     public void log(Marker marker, String fqcn, int level, String message,
             Object[] argArray, Throwable t) {
         SlingLoggerLevel slingLevel = SlingLoggerLevel.fromSlf4jLevel(level);
-        if (config.isLevel(slingLevel)) {
+        if (config.isLevel(slingLevel) && config.markerPasses(marker)) {
             log(marker, fqcn, slingLevel,
                 MessageFormatter.format(message, argArray, t));
         }
     }
-}
+}
\ No newline at end of file

Modified: sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerConfig.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerConfig.java?rev=1449656&r1=1449655&r2=1449656&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerConfig.java
(original)
+++ sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerConfig.java
Mon Feb 25 10:51:06 2013
@@ -38,22 +38,28 @@ class SlingLoggerConfig {
 
     private Set<String> categories;
 
+    private Set<String> markers;
+
     private SlingLoggerLevel level;
 
     private MessageFormat format;
 
     private SlingLoggerWriter writer;
 
-    SlingLoggerConfig(String pid, String pattern, Set<String> categories,
+    SlingLoggerConfig(String pid, String pattern, Set<String> categories, Set<String>
markers,
             SlingLoggerLevel level, SlingLoggerWriter writer) {
         this.configPid = pid;
-        configure(pattern, categories, level, writer);
+        configure(pattern, categories, markers, level, writer);
     }
 
-    void configure(String pattern, Set<String> categories,
+    void configure(String pattern, Set<String> categories, Set<String> markers,
             SlingLoggerLevel level, SlingLoggerWriter writer) {
         this.format = new MessageFormat(pattern);
         this.categories = new HashSet<String>(categories);
+        this.markers = new HashSet<String>();
+        if(markers != null) {
+            this.markers.addAll(markers);
+        }
         this.level = level;
         this.writer = writer;
     }
@@ -70,6 +76,10 @@ class SlingLoggerConfig {
         return categories;
     }
 
+    Set<String> getMarkers() {
+        return markers;
+    }
+
     SlingLoggerWriter getLogWriter() {
         return writer;
     }
@@ -85,6 +95,26 @@ class SlingLoggerConfig {
     boolean isLevel(SlingLoggerLevel reference) {
         return level.compareTo(reference) <= 0;
     }
+    
+    /** Find out if message having specified marker
+     *  must be logged
+     * @param marker can be null
+     * @return true if a message with supplied marker
+     *  must be logged according to this config.
+     */
+    boolean markerPasses(Marker marker) {
+        if(markers == null || markers.isEmpty()) {
+            // no markers -> everything goes through
+            return true;
+        }
+        
+        if(marker == null) {
+            // markers -> only marked messages pass through
+            return false;
+        } else {
+            return markers.contains(marker.getName());
+        }
+    }
 
     void setLogLevel(SlingLoggerLevel level) {
         this.level = level;

Modified: sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1449656&r1=1449655&r2=1449656&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.properties
(original)
+++ sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.properties
Mon Feb 25 10:51:06 2013
@@ -67,3 +67,8 @@ log.loggers.description = The logger nam
  otherwise. E.g. a logger name of org.apache.sling applies to logger \
  org.apache.sling.commons unless there is a different configuration for \
  org.apache.sling.commons. 
+
+log.markers.name = Markers
+log.markers.description = If not empty, the logger only logs messages \
+  having a marker with a name that matches one of the specified markers. 
+ 
\ No newline at end of file

Modified: sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.xml?rev=1449656&r1=1449655&r2=1449656&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.xml
(original)
+++ sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/main/resources/OSGI-INF/metatype/metatype.xml
Mon Feb 25 10:51:06 2013
@@ -101,6 +101,10 @@
             type="String" name="%log.loggers.name"
             description="%log.loggers.description"
             cardinality="2147483647" />
+        <metatype:AD id="org.apache.sling.commons.log.markers"
+            type="String" name="%log.markers.name"
+            description="%log.markers.description"
+            cardinality="2147483647" />
     </metatype:OCD>
     <metatype:Designate
         pid="org.apache.sling.commons.log.LogManager.factory.config"

Modified: sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java?rev=1449656&r1=1449655&r2=1449656&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java
(original)
+++ sling/whiteboard/bdelacretaz/bundles-commons-log-with-markers/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java
Mon Feb 25 10:51:06 2013
@@ -23,11 +23,58 @@ import java.io.StringWriter;
 import java.io.Writer;
 import java.lang.reflect.Method;
 import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
 
 import junit.framework.TestCase;
 
+import org.slf4j.Marker;
+
 public class SlingLoggerTest extends TestCase {
+    
+    private static final Set<String> EMPTY_SET = new HashSet<String>();
+
+    @SuppressWarnings("serial")
+    static class TestMarker implements Marker {
+        private final String name;
+        
+        TestMarker(String name) {
+            this.name = name;
+        }
+
+        public void add(Marker arg0) {
+        }
+
+        public boolean contains(Marker arg0) {
+            return false;
+        }
+
+        public boolean contains(String arg0) {
+            return false;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public boolean hasChildren() {
+            return false;
+        }
 
+        public boolean hasReferences() {
+            return false;
+        }
+
+        public Iterator<?> iterator() {
+            return null;
+        }
+
+        public boolean remove(Marker arg0) {
+            return false;
+        }
+        
+    }
+    
     private SlingLoggerWriter output = new SlingLoggerWriter(null) {
         {
             try {
@@ -54,7 +101,7 @@ public class SlingLoggerTest extends Tes
         super.setUp();
 
         config = new SlingLoggerConfig(getClass().getName(), "",
-            new HashSet<String>(), SlingLoggerLevel.DEBUG, output);
+            EMPTY_SET, EMPTY_SET, SlingLoggerLevel.DEBUG, output);
 
         logger = new SlingLogger("sample");
         logger.setLoggerConfig(config);
@@ -147,7 +194,7 @@ public class SlingLoggerTest extends Tes
         setDelagateeOn(output, w);
 
         // a single message
-        config.configure(messageOnly, new HashSet<String>(),
+        config.configure(messageOnly, EMPTY_SET, EMPTY_SET, 
             SlingLoggerLevel.DEBUG, output);
 
         String message = "This is a message";
@@ -155,8 +202,8 @@ public class SlingLoggerTest extends Tes
         assertEquals(message, w.toString());
 
         // reset output buffer and format with logger name and message
-        w.getBuffer().delete(0, w.getBuffer().length());
-        config.configure("{3}|{5}", new HashSet<String>(),
+        clear(w);
+        config.configure("{3}|{5}", EMPTY_SET, EMPTY_SET, 
             SlingLoggerLevel.DEBUG, output);
 
         logger.warn(message);
@@ -164,14 +211,60 @@ public class SlingLoggerTest extends Tes
 
         // reset output buffer and format with logger name, level, thread and
         // message
-        w.getBuffer().delete(0, w.getBuffer().length());
-        config.configure("{2}|{3}|{4}|{5}", new HashSet<String>(),
+        clear(w);
+        config.configure("{2}|{3}|{4}|{5}", EMPTY_SET, EMPTY_SET, 
             SlingLoggerLevel.DEBUG, output);
         logger.warn(message);
         assertEquals(Thread.currentThread().getName() + "|" + logger.getName()
             + "|" + SlingLoggerLevel.WARN + "|" + message, w.toString());
     }
-
+    
+    public void testMarkers() {
+        final StringWriter w = new StringWriter();
+        setDelagateeOn(output, w);
+        final Set<String> markers = new HashSet<String>();
+        
+        config.configure(messageOnly, EMPTY_SET, 
+                markers, SlingLoggerLevel.INFO, output);
+        
+        clear(w);
+        logger.info("something");
+        assertFalse("No markers -> info writes to log", w.toString().isEmpty());
+        
+        markers.add("foo");
+        markers.add("bar");
+        config.configure(messageOnly, EMPTY_SET, 
+                markers, SlingLoggerLevel.INFO, output);
+        
+        clear(w);
+        logger.debug("More");
+        assertTrue("Markers + debug without marker -> no message", w.toString().isEmpty());
+        
+        clear(w);
+        logger.info("More");
+        assertTrue("Markers + info without marker -> no message", w.toString().isEmpty());
+        
+        clear(w);
+        logger.info(new TestMarker("foo"), "yes");
+        assertFalse("Markers + log with foo marker -> message", w.toString().isEmpty());
+        
+        clear(w);
+        logger.info(new TestMarker("bar"), "again");
+        assertFalse("Markers + log with bar marker -> message", w.toString().isEmpty());
+
+        clear(w);
+        logger.debug(new TestMarker("bar"), "again");
+        assertTrue("Markers + log with bar marker at debug level -> no message", w.toString().isEmpty());
+
+        clear(w);
+        logger.info(new TestMarker("other"), "not");
+        assertTrue("Markers + log with other marker -> no message", w.toString().isEmpty());
+    }
+    
+    private static void clear(StringWriter w) {
+        w.getBuffer().delete(0, w.getBuffer().length());
+    }
+    
     private static void setDelagateeOn(SlingLoggerWriter output, Writer w) {
         try {
             Method delegateeSetter = SlingLoggerWriter.class.getDeclaredMethod(



Mime
View raw message