logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1585355 - in /logging/log4j/log4j2/trunk: log4j-api/src/main/java/org/apache/logging/log4j/ log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/ log4j-slf4j-impl/src/main/j...
Date Sun, 06 Apr 2014 22:01:43 GMT
Author: rgoers
Date: Sun Apr  6 22:01:42 2014
New Revision: 1585355

URL: http://svn.apache.org/r1585355
Log:
LOG4J2-585 - Add support for multiple parents to Markers

Added:
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java
Removed:
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/slf4j/helpers/MarkerWrapper.java
Modified:
    logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/Marker.java
    logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/MarkerManager.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/MarkerFilterTest.java
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/resources/log4j-test1.xml
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/markers.xml

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/Marker.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/Marker.java?rev=1585355&r1=1585354&r2=1585355&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/Marker.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/Marker.java
Sun Apr  6 22:01:42 2014
@@ -34,12 +34,19 @@ public interface Marker extends Serializ
     String getName();
 
     /**
-     * Returns the parent of this Marker.
-     * @return The parent Marker or null if this Marker has no parent.
+     * Returns the first parent of this Marker.
+     * @return The first parent Marker or null if this Marker has no parents.
+     * @deprecated Use getParents() instead.
      */
     Marker getParent();
 
     /**
+     * Returns the list of parents of this Marker.
+     * @return The parent Markers or null if this Marker has no parents.
+     */
+    Marker[] getParents();
+
+    /**
      * Checks whether this Marker is an instance of the specified Marker.
      * @param m The Marker to check.
      * @return true of this Marker or one of its ancestors is the specified Marker, false
otherwise.
@@ -52,4 +59,17 @@ public interface Marker extends Serializ
      * @return true of this Marker or one of its ancestors matches the specified name, false
otherwise.
      */
     boolean isInstanceOf(String name);
+
+    /**
+     * Adds a Marker as a parent to this Marker.
+     * @param marker The parent marker to add.
+     */
+    void add(Marker marker);
+
+    /**
+     * Removes the specified Marker as a parent of this Marker.
+     * @param marker The marker to remove.
+     * @return true if the marker was removed.
+     */
+    boolean remove(Marker marker);
 }

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/MarkerManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/MarkerManager.java?rev=1585355&r1=1585354&r2=1585355&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/MarkerManager.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/MarkerManager.java
Sun Apr  6 22:01:42 2014
@@ -16,6 +16,8 @@
  */
 package org.apache.logging.log4j;
 
+import java.util.HashSet;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -68,6 +70,17 @@ public final class MarkerManager {
     }
 
     /**
+     * Retrieves or creates a Marker with the specified parents.
+     * @param name The name of the Marker.
+     * @param parents The parent Markers.
+     * @return The Marker with the specified name.
+     */
+    public static Marker getMarker(final String name, final Marker... parents) {
+        markerMap.putIfAbsent(name, new Log4jMarker(name, parents));
+        return markerMap.get(name);
+    }
+
+    /**
      * The actual Marker implementation.
      */
     private static class Log4jMarker implements Marker {
@@ -75,16 +88,74 @@ public final class MarkerManager {
         private static final long serialVersionUID = 100L;
 
         private final String name;
-        private final Marker parent;
+        private volatile Marker[] parents;
 
         public Log4jMarker(final String name) {
             this.name = name;
-            this.parent = null;
+            this.parents = null;
         }
 
         public Log4jMarker(final String name, final Marker parent) {
             this.name = name;
-            this.parent = parent;
+            this.parents = parent != null ? new Marker[] {parent} : null;
+        }
+
+        public Log4jMarker(final String name, final Marker... parents) {
+            this.name = name;
+            for (Marker marker : parents) {
+                if (marker == null) {
+                    throw new IllegalArgumentException("Marker cannot contain a null parent");
+                }
+            }
+            this.parents = parents;
+        }
+
+        @Override
+        public synchronized void add(Marker parent) {
+            if (parent == null) {
+                throw new IllegalArgumentException("A parent marker must be specified");
+            }
+            // Don't add a parent that is already in the hierarchy.
+            if (parents != null && (this.isInstanceOf(parent) || parent.isInstanceOf(this)))
{
+                return;
+            }
+            int size = parents == null ? 1 : parents.length + 1;
+            Marker[] markers = new Marker[size];
+            if (parents != null) {
+                System.arraycopy(parents, 0, markers, 0, parents.length);
+            }
+            markers[size - 1] = parent;
+            parents = markers;
+        }
+
+        @Override
+        public synchronized boolean remove(Marker parent) {
+            if (parent == null) {
+                throw new IllegalArgumentException("A parent marker must be specified");
+            }
+            if (parents == null) {
+                return false;
+            }
+            if (parents.length == 1) {
+                if (parents[0].equals(parent)) {
+                    parents = null;
+                    return true;
+                }
+                return false;
+            }
+            int index = 0;
+            Marker[] markers = new Marker[parents.length - 1];
+            for (int i = 0; i < parents.length; ++i) {
+                Marker marker = parents[i];
+                if (!marker.equals(parent)) {
+                    if (index == parents.length - 1) {
+                        return false;
+                    }
+                    markers[index++] = marker;
+                }
+            }
+            parents = markers;
+            return true;
         }
 
         @Override
@@ -94,7 +165,12 @@ public final class MarkerManager {
 
         @Override
         public Marker getParent() {
-            return this.parent;
+            return this.parents != null ? parents[0] : null;
+        }
+
+        @Override
+        public Marker[] getParents() {
+            return this.parents;
         }
 
         @Override
@@ -102,13 +178,24 @@ public final class MarkerManager {
             if (marker == null) {
                 throw new IllegalArgumentException("A marker parameter is required");
             }
-            Marker test = this;
-            do {
-                if (test == marker) {
-                    return true;
+            if (this == marker) {
+                return true;
+            }
+            Marker[] localParents = parents;
+            if (localParents != null) {
+                // With only one or two parents the for loop is slower.
+                if (localParents.length == 1) {
+                    return checkParent(localParents[0], marker);
                 }
-                test = test.getParent();
-            } while (test != null);
+                if (localParents.length == 2) {
+                    return checkParent(localParents[0], marker) || checkParent(localParents[1],
marker);
+                }
+                for (int i = 0; i < localParents.length; ++i) {
+                    if (checkParent(localParents[i], marker)) {
+                        return true;
+                    }
+                }
+            }
             return false;
         }
 
@@ -117,13 +204,50 @@ public final class MarkerManager {
             if (markerName == null) {
                 throw new IllegalArgumentException("A marker name is required");
             }
-            Marker toTest = this;
-            do {
-                if (markerName.equals(toTest.getName())) {
-                    return true;
+            if (markerName.equals(this.getName())) {
+                return true;
+            }
+            // Use a real marker for child comparisons. It is faster than comparing the names.
+            Marker marker = markerMap.get(markerName);
+            if (marker == null) {
+                throw new IllegalArgumentException("No marker exists with the name " + markerName);
+            }
+            Marker[] localParents = parents;
+            if (localParents != null) {
+                if (localParents.length == 1) {
+                    return checkParent(localParents[0], marker);
+                }
+                if (localParents.length == 2) {
+                    return checkParent(localParents[0], marker) || checkParent(localParents[1],
marker);
+                }
+                for (int i = 0; i < localParents.length; ++i) {
+                    if (checkParent(localParents[i], marker)) {
+                        return true;
+                    }
+                }
+            }
+
+            return false;
+        }
+
+        private boolean checkParent(Marker parent, Marker marker) {
+            if (parent == marker) {
+                return true;
+            }
+            Marker[] localParents = parent.getParents();
+            if (localParents != null) {
+                if (localParents.length == 1) {
+                    return checkParent(localParents[0], marker);
+                }
+                if (localParents.length == 2) {
+                    return checkParent(localParents[0], marker) || checkParent(localParents[1],
marker);
+                }
+                for (int i = 0; i < localParents.length; ++i) {
+                    if (checkParent(localParents[i], marker)) {
+                        return true;
+                    }
                 }
-                toTest = toTest.getParent();
-            } while (toTest != null);
+            }
             return false;
         }
 
@@ -153,21 +277,28 @@ public final class MarkerManager {
         @Override
         public String toString() {
             final StringBuilder sb = new StringBuilder(name);
-            if (parent != null) {
-                Marker m = parent;
-                sb.append("[ ");
-                boolean first = true;
-                while (m != null) {
-                    if (!first) {
-                        sb.append(", ");
-                    }
-                    sb.append(m.getName());
-                    first = false;
-                    m = m.getParent();
-                }
-                sb.append(" ]");
+            Marker[] localParents = parents;
+            if (localParents != null) {
+                addParentInfo(localParents, sb);
             }
             return sb.toString();
         }
+
+        private void addParentInfo(Marker[] parents, StringBuilder sb) {
+            sb.append("[ ");
+            boolean first = true;
+            for (Marker marker : parents) {
+                if (!first) {
+                    sb.append(", ");
+                }
+                first = false;
+                sb.append(marker.getName());
+                Marker[] p = marker.getParents();
+                if (p != null) {
+                    addParentInfo(p, sb);
+                }
+            }
+            sb.append(" ]");
+        }
     }
 }

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/MarkerFilterTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/MarkerFilterTest.java?rev=1585355&r1=1585354&r2=1585355&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/MarkerFilterTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/MarkerFilterTest.java
Sun Apr  6 22:01:42 2014
@@ -44,10 +44,15 @@ public class MarkerFilterTest {
         assertTrue(filter.isStarted());
         assertTrue(filter.filter(null, null, stranger, null, (Throwable)null) == Filter.Result.DENY);
         assertTrue(filter.filter(null, null, child, null, (Throwable)null) == Filter.Result.NEUTRAL);
+        assertTrue(filter.filter(null, null, grandChild, null, (Throwable)null) == Filter.Result.NEUTRAL);
+        filter.stop();
         LogEvent event = new Log4jLogEvent(null, grandChild, null, Level.DEBUG, new SimpleMessage("Test"),
null);
+        assertTrue(filter.filter(event) == Filter.Result.NEUTRAL);
         filter = MarkerFilter.createFilter("Child", null, null);
+        filter.start();
         assertTrue(filter.filter(event) == Filter.Result.NEUTRAL);
         event = new Log4jLogEvent(null, sibling, null, Level.DEBUG, new SimpleMessage("Test"),
null);
         assertTrue(filter.filter(event) == Filter.Result.DENY);
+        filter.stop();
     }
 }

Modified: logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java?rev=1585355&r1=1585354&r2=1585355&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java
Sun Apr  6 22:01:42 2014
@@ -37,7 +37,7 @@ import org.slf4j.spi.LocationAwareLogger
 public class Log4jLogger implements LocationAwareLogger, Serializable {
 
     public static final String FQCN = Log4jLogger.class.getName();
-    
+
     private static final long serialVersionUID = 7869000638091304316L;
     private static final Marker EVENT_MARKER = MarkerFactory.getMarker("EVENT");
     private final boolean eventLogger;
@@ -84,32 +84,32 @@ public class Log4jLogger implements Loca
 
     @Override
     public boolean isTraceEnabled(final Marker marker) {
-        return logger.isEnabled(Level.TRACE, (org.apache.logging.log4j.Marker) marker, null);
+        return logger.isEnabled(Level.TRACE, getMarker(marker), null);
     }
 
     @Override
     public void trace(final Marker marker, final String s) {
-        logger.logIfEnabled(FQCN, Level.TRACE, (org.apache.logging.log4j.Marker) marker,
s);
+        logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s);
     }
 
     @Override
     public void trace(final Marker marker, final String s, final Object o) {
-        logger.logIfEnabled(FQCN, Level.TRACE, (org.apache.logging.log4j.Marker) marker,
s, o);
+        logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s, o);
     }
 
     @Override
     public void trace(final Marker marker, final String s, final Object o, final Object o1)
{
-        logger.logIfEnabled(FQCN, Level.TRACE, (org.apache.logging.log4j.Marker) marker,
s, o, o1);
+        logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s, o, o1);
     }
 
     @Override
     public void trace(final Marker marker, final String s, final Object... objects) {
-        logger.logIfEnabled(FQCN, Level.TRACE, (org.apache.logging.log4j.Marker) marker,
s, objects);
+        logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s, objects);
     }
 
     @Override
     public void trace(final Marker marker, final String s, final Throwable throwable) {
-        logger.logIfEnabled(FQCN, Level.TRACE, (org.apache.logging.log4j.Marker) marker,
s, throwable);
+        logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s, throwable);
     }
 
     @Override
@@ -144,32 +144,32 @@ public class Log4jLogger implements Loca
 
     @Override
     public boolean isDebugEnabled(final Marker marker) {
-        return logger.isEnabled(Level.DEBUG, (org.apache.logging.log4j.Marker) marker, null);
+        return logger.isEnabled(Level.DEBUG, getMarker(marker), null);
     }
 
     @Override
     public void debug(final Marker marker, final String s) {
-        logger.logIfEnabled(FQCN, Level.DEBUG, (org.apache.logging.log4j.Marker) marker,
s);
+        logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s);
     }
 
     @Override
     public void debug(final Marker marker, final String s, final Object o) {
-        logger.logIfEnabled(FQCN, Level.DEBUG, (org.apache.logging.log4j.Marker) marker,
s, o);
+        logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s, o);
     }
 
     @Override
     public void debug(final Marker marker, final String s, final Object o, final Object o1)
{
-        logger.logIfEnabled(FQCN, Level.DEBUG, (org.apache.logging.log4j.Marker) marker,
s, o, o1);
+        logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s, o, o1);
     }
 
     @Override
     public void debug(final Marker marker, final String s, final Object... objects) {
-        logger.logIfEnabled(FQCN, Level.DEBUG, (org.apache.logging.log4j.Marker) marker,
s, objects);
+        logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s, objects);
     }
 
     @Override
     public void debug(final Marker marker, final String s, final Throwable throwable) {
-        logger.logIfEnabled(FQCN, Level.DEBUG, (org.apache.logging.log4j.Marker) marker,
s, throwable);
+        logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s, throwable);
     }
 
     @Override
@@ -204,32 +204,32 @@ public class Log4jLogger implements Loca
 
     @Override
     public boolean isInfoEnabled(final Marker marker) {
-        return logger.isEnabled(Level.INFO, (org.apache.logging.log4j.Marker) marker, null);
+        return logger.isEnabled(Level.INFO, getMarker(marker), null);
     }
 
     @Override
     public void info(final Marker marker, final String s) {
-        logger.logIfEnabled(FQCN, Level.INFO, (org.apache.logging.log4j.Marker) marker, s);
+        logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s);
     }
 
     @Override
     public void info(final Marker marker, final String s, final Object o) {
-        logger.logIfEnabled(FQCN, Level.INFO, (org.apache.logging.log4j.Marker) marker, s,
o);
+        logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s, o);
     }
 
     @Override
     public void info(final Marker marker, final String s, final Object o, final Object o1)
{
-        logger.logIfEnabled(FQCN, Level.INFO, (org.apache.logging.log4j.Marker) marker, s,
o, o1);
+        logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s, o, o1);
     }
 
     @Override
     public void info(final Marker marker, final String s, final Object... objects) {
-        logger.logIfEnabled(FQCN, Level.INFO, (org.apache.logging.log4j.Marker) marker, s,
objects);
+        logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s, objects);
     }
 
     @Override
     public void info(final Marker marker, final String s, final Throwable throwable) {
-        logger.logIfEnabled(FQCN, Level.INFO, (org.apache.logging.log4j.Marker) marker, s,
throwable);
+        logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s, throwable);
     }
 
     @Override
@@ -264,32 +264,32 @@ public class Log4jLogger implements Loca
 
     @Override
     public boolean isWarnEnabled(final Marker marker) {
-        return logger.isEnabled(Level.WARN, (org.apache.logging.log4j.Marker) marker, null);
+        return logger.isEnabled(Level.WARN, getMarker(marker), null);
     }
 
     @Override
     public void warn(final Marker marker, final String s) {
-        logger.logIfEnabled(FQCN, Level.WARN, (org.apache.logging.log4j.Marker) marker, s);
+        logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s);
     }
 
     @Override
     public void warn(final Marker marker, final String s, final Object o) {
-        logger.logIfEnabled(FQCN, Level.WARN, (org.apache.logging.log4j.Marker) marker, s,
o);
+        logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s, o);
     }
 
     @Override
     public void warn(final Marker marker, final String s, final Object o, final Object o1)
{
-        logger.logIfEnabled(FQCN, Level.WARN, (org.apache.logging.log4j.Marker) marker, s,
o, o1);
+        logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s, o, o1);
     }
 
     @Override
     public void warn(final Marker marker, final String s, final Object... objects) {
-        logger.logIfEnabled(FQCN, Level.WARN, (org.apache.logging.log4j.Marker) marker, s,
objects);
+        logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s, objects);
     }
 
     @Override
     public void warn(final Marker marker, final String s, final Throwable throwable) {
-        logger.logIfEnabled(FQCN, Level.WARN, (org.apache.logging.log4j.Marker) marker, s,
throwable);
+        logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s, throwable);
     }
 
     @Override
@@ -324,39 +324,39 @@ public class Log4jLogger implements Loca
 
     @Override
     public boolean isErrorEnabled(final Marker marker) {
-        return logger.isEnabled(Level.ERROR, (org.apache.logging.log4j.Marker) marker, null);
+        return logger.isEnabled(Level.ERROR, getMarker(marker), null);
     }
 
     @Override
     public void error(final Marker marker, final String s) {
-        logger.logIfEnabled(FQCN, Level.ERROR, (org.apache.logging.log4j.Marker) marker,
s);
+        logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s);
     }
 
     @Override
     public void error(final Marker marker, final String s, final Object o) {
-        logger.logIfEnabled(FQCN, Level.ERROR, (org.apache.logging.log4j.Marker) marker,
s, o);
+        logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s, o);
     }
 
     @Override
     public void error(final Marker marker, final String s, final Object o, final Object o1)
{
-        logger.logIfEnabled(FQCN, Level.ERROR, (org.apache.logging.log4j.Marker) marker,
s, o, o1);
+        logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s, o, o1);
     }
 
     @Override
     public void error(final Marker marker, final String s, final Object... objects) {
-        logger.logIfEnabled(FQCN, Level.ERROR, (org.apache.logging.log4j.Marker) marker,
s, objects);
+        logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s, objects);
     }
 
     @Override
     public void error(final Marker marker, final String s, final Throwable throwable) {
-        logger.logIfEnabled(FQCN, Level.ERROR, (org.apache.logging.log4j.Marker) marker,
s, throwable);
+        logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s, throwable);
     }
 
     @Override
     public void log(final Marker marker, final String fqcn, final int level, final String
message, final Object[] params, Throwable throwable) {
         final Level log4jLevel = getLevel(level);
-        final org.apache.logging.log4j.Marker log4jMarker = (org.apache.logging.log4j.Marker)
marker;
-        
+        final org.apache.logging.log4j.Marker log4jMarker = getMarker(marker);
+
         if (!logger.isEnabled(log4jLevel, log4jMarker, message, params)) {
             return;
         }
@@ -374,6 +374,10 @@ public class Log4jLogger implements Loca
         logger.logMessage(fqcn, log4jLevel, log4jMarker, msg, throwable);
     }
 
+    private org.apache.logging.log4j.Marker getMarker(Marker marker) {
+        return marker != null ? ((org.apache.logging.slf4j.Log4jMarker) marker).getLog4jMarker()
: null;
+    }
+
     @Override
     public String getName() {
         return name;

Added: logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java?rev=1585355&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java
Sun Apr  6 22:01:42 2014
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.slf4j;
+
+import org.apache.logging.log4j.MarkerManager;
+import org.slf4j.IMarkerFactory;
+import org.slf4j.Marker;
+import org.slf4j.impl.StaticMarkerBinder;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ *
+ */
+public class Log4jMarker implements Marker {
+
+    private final IMarkerFactory factory = StaticMarkerBinder.SINGLETON.getMarkerFactory();
+
+    private final org.apache.logging.log4j.Marker marker;
+
+    public Log4jMarker(org.apache.logging.log4j.Marker marker) {
+        this.marker = marker;
+    }
+
+    public org.apache.logging.log4j.Marker getLog4jMarker() {
+        return marker;
+    }
+
+    @Override
+    public void add(Marker marker) {
+        Marker m = factory.getMarker(marker.getName());
+        this.marker.add(((Log4jMarker)m).getLog4jMarker());
+    }
+
+    @Override
+    public boolean remove(Marker marker) {
+        return this.marker.remove(MarkerManager.getMarker(marker.getName()));
+    }
+
+    @Override
+    public String getName() {
+        return marker.getName();
+    }
+
+    @Override
+    public boolean hasReferences() {
+        return marker.getParents() != null;
+    }
+
+    @Override
+    public boolean hasChildren() {
+        return marker.getParents() != null;
+    }
+
+    @Override
+    public Iterator iterator() {
+        List<Marker> parents = new ArrayList<Marker>();
+        for (org.apache.logging.log4j.Marker m : this.marker.getParents()) {
+            parents.add(factory.getMarker(m.getName()));
+        }
+        return parents.iterator();
+    }
+
+    @Override
+    public boolean contains(org.slf4j.Marker marker) {
+        return this.marker.isInstanceOf(marker.getName());
+    }
+
+    @Override
+    public boolean contains(String s) {
+        return this.marker.isInstanceOf(s);
+    }
+}

Modified: logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java?rev=1585355&r1=1585354&r2=1585355&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java
Sun Apr  6 22:01:42 2014
@@ -19,9 +19,9 @@ package org.apache.logging.slf4j;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
+import org.apache.logging.log4j.MarkerManager;
 import org.slf4j.IMarkerFactory;
 import org.slf4j.Marker;
-import org.slf4j.helpers.MarkerWrapper;
 
 /**
  *
@@ -30,6 +30,11 @@ public class Log4jMarkerFactory implemen
 
     private final ConcurrentMap<String, Marker> markerMap = new ConcurrentHashMap<String,
Marker>();
 
+    /**
+     * Return a Log4j Marker that is compatible with SLF4J.
+     * @param name The name of the Marker.
+     * @return A Marker.
+     */
     @Override
     public Marker getMarker(final String name) {
         if (name == null) {
@@ -39,23 +44,41 @@ public class Log4jMarkerFactory implemen
         if (marker != null) {
             return marker;
         }
-        marker = new MarkerWrapper(name);
+        org.apache.logging.log4j.Marker log4jMarker = MarkerManager.getMarker(name);
+        marker = new Log4jMarker(log4jMarker);
         final Marker existing = markerMap.putIfAbsent(name, marker);
         return existing == null ? marker : existing;
     }
 
+    /**
+     * Returns true if the Marker exists.
+     * @param name The Marker name.
+     * @return true if the Marker exists, false otherwise.
+     */
     @Override
     public boolean exists(final String name) {
         return markerMap.containsKey(name);
     }
 
+    /**
+     * Log4j does not support detached Markers. This method always returns false.
+     * @param name The Marker name.
+     * @return false
+     */
     @Override
     public boolean detachMarker(final String name) {
         return false;
     }
 
+    /**
+     * Log4j does not support detached Markers for performance reasons. The returned Marker
is attached.
+     * @param name The Marker name.
+     * @return
+     */
     @Override
     public Marker getDetachedMarker(final String name) {
-        return new MarkerWrapper(name);
+        return getMarker(name);
     }
+
+
 }

Modified: logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java?rev=1585355&r1=1585354&r2=1585355&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java
Sun Apr  6 22:01:42 2014
@@ -19,7 +19,6 @@ package org.apache.logging.slf4j;
 import org.apache.logging.log4j.Marker;
 import org.apache.logging.log4j.MarkerManager;
 import org.junit.Test;
-import org.slf4j.helpers.MarkerWrapper;
 
 import static org.junit.Assert.assertTrue;
 
@@ -34,10 +33,10 @@ public class MarkerTest {
         final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker("PARENT");
         slf4jMarker.add(slf4jParent);
         final Marker log4jParent = MarkerManager.getMarker("PARENT");
-        final Marker log4jMarker = MarkerManager.getMarker("TEST", log4jParent);
+        final Marker log4jMarker = MarkerManager.getMarker("TEST");
 
-        assertTrue("Incorrect Marker class", slf4jMarker instanceof MarkerWrapper);
-        assertTrue("SLF4J Marker doesn't match Log4j Marker", ((MarkerWrapper) slf4jMarker).isInstanceOf(log4jMarker));
-        assertTrue("SLF4J Parent doesn't match Log4j parent", ((MarkerWrapper) slf4jMarker).isInstanceOf(log4jParent));
+        assertTrue("Incorrect Marker class", slf4jMarker instanceof Log4jMarker);
+        assertTrue("TEST is not an instance of PARENT in Log4j", log4jMarker.isInstanceOf(log4jParent));
+        assertTrue("TEST is not an instance of PARENT in SLF4J", slf4jMarker.contains(slf4jParent));
     }
 }

Modified: logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/resources/log4j-test1.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/resources/log4j-test1.xml?rev=1585355&r1=1585354&r2=1585355&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/resources/log4j-test1.xml (original)
+++ logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/resources/log4j-test1.xml Sun Apr
 6 22:01:42 2014
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<configuration packages="org.apache.logging.log4j.test" status="debug" name="LoggerTest">
+<configuration packages="org.apache.logging.log4j.test" status="error" name="LoggerTest">
   <properties>
     <property name="filename">target/test.log</property>
   </properties>

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1585355&r1=1585354&r2=1585355&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sun Apr  6 22:01:42 2014
@@ -21,6 +21,9 @@
   </properties>
   <body>
     <release version="2.0-rc2" date="2014-MM-DD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-585" dev="rgoers" type="update">
+        Add support for multiple parents to Markers.
+      </action>
       <action issue="LOG4J2-595" dev="mattsicker" type="add">
           Introduce Java annotation processor as alternative plugin pre-caching mechanism.
This is available in the
           log4j-plugin-processor module.

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/markers.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/markers.xml?rev=1585355&r1=1585354&r2=1585355&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/markers.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/markers.xml Sun Apr  6 22:01:42 2014
@@ -86,10 +86,13 @@ public class MyApp {
                 <li>Markers must be unique. They are permanently registered by name
so care should be taken
                   to insure that Markers used in your application are distinct from those
in the application's
                   dependencies, unless that is what is desired.</li>
-                <li>Like many objects in Log4j 2, Markers are immutable. If a marker
has a parent, the parent
-                  must be named when the Marker is created and the parent cannot be changed
after creation.</li>
-                <li>Markers only have a single parent. Markers can, however, have many
children that reference
-                  them.</li>
+                <li>Parent Markers can be added or removed dynamically. However, this
is fairly expensive to do.
+                  Instead, it is recommended that the parents be identified when obtaining
the Marker the first time
+                  as shown in the examples above.</li>
+                <li>Evaluating Markers with multiple ancestors is much more expensive
than Markers with no parents.
+                  For example, in one set of tests to evaluate whether a Marker matched its
grandparent took 3
+                  times longer than evaluating the Marker itself. Even then though, evaluating
Markers is
+                  inexpensive compared resolving the callers class name or line number.</li>
               </ol>
             </p>
           </subsection>



Mime
View raw message