logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1592286 - in /logging/log4j/log4j2/trunk: log4j-api/src/main/java/org/apache/logging/log4j/ log4j-api/src/main/java/org/apache/logging/log4j/spi/ log4j-api/src/test/java/org/apache/logging/log4j/ log4j-core/src/main/java/org/apache/logging...
Date Sat, 03 May 2014 22:10:59 GMT
Author: rgoers
Date: Sat May  3 22:10:58 2014
New Revision: 1592286

URL: http://svn.apache.org/r1592286
Log:
Change Marker.add to Marker.addParents - add unit tests and remove use of deprecated methods

Added:
    logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/MarkerTest.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-api/src/main/java/org/apache/logging/log4j/spi/AbstractLoggerProvider.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverter.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverterTest.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/MarkerFilterTest.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java

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=1592286&r1=1592285&r2=1592286&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
Sat May  3 22:10:58 2014
@@ -71,11 +71,11 @@ public interface Marker extends Serializ
 
     /**
      * Adds a Marker as a parent to this Marker.
-     * @param marker The parent marker to add.
+     * @param markers The parent markers to add.
      * @return The current Marker object, thus allowing multiple adds to be concatenated.
      * @throws IllegalArgumentException if the argument is {@code null}
      */
-    Marker add(Marker marker);
+    Marker addParents(Marker... markers);
 
     /**
      * Replaces the set of parent Markers with the provided Markers.

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=1592286&r1=1592285&r2=1592286&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
Sat May  3 22:10:58 2014
@@ -73,7 +73,7 @@ public final class MarkerManager {
     @Deprecated
     public static Marker getMarker(final String name, final Marker parent) {
         markerMap.putIfAbsent(name, new Log4jMarker(name));
-        return markerMap.get(name).add(parent);
+        return markerMap.get(name).addParents(parent);
     }
     /**
      * The actual Marker implementation.
@@ -103,31 +103,43 @@ public final class MarkerManager {
         // TODO: use java.util.concurrent
 
         @Override
-        public synchronized Marker add(final Marker parent) {
-            if (parent == null) {
+        public synchronized Marker addParents(final Marker... parents) {
+            if (parents == null) {
                 throw new IllegalArgumentException("A parent marker must be specified");
             }
             // It is not strictly necessary to copy the variable here but it should perform
better than
             // Accessing a volatile variable multiple times.
             final Marker[] localParents = this.parents;
             // Don't add a parent that is already in the hierarchy.
-            if (localParents != null && (contains(parent, localParents) || parent.isInstanceOf(this)))
{
-                return this;
+            int count = 0;
+            int size = parents.length;
+            if (localParents != null) {
+                for (Marker parent : parents) {
+                    if (localParents != null && !(contains(parent, localParents)
|| parent.isInstanceOf(this))) {
+                        ++count;
+                    }
+                }
+                if (count == 0) {
+                    return this;
+                }
+                size = localParents.length + count;
             }
-            final int size = localParents == null ? 1 : localParents.length + 1;
             final Marker[] markers = new Marker[size];
             if (localParents != null) {
                 // It's perfectly OK to call arraycopy in a synchronized context; it's still
faster
                 //noinspection CallToNativeMethodWhileLocked
                 System.arraycopy(localParents, 0, markers, 0, localParents.length);
             }
-            markers[size - 1] = parent;
+            int index = localParents == null ? 0 : localParents.length;
+            for (Marker parent : parents) {
+                if (localParents == null || !(contains(parent, localParents) || parent.isInstanceOf(this)))
{
+                    markers[index++] = parent;
+                }
+            }
             this.parents = markers;
             return this;
         }
 
-        // TODO: add(Marker parent, Marker... moreParents)
-
         @Override
         public synchronized boolean remove(final Marker parent) {
             if (parent == null) {

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLoggerProvider.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLoggerProvider.java?rev=1592286&r1=1592285&r2=1592286&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLoggerProvider.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLoggerProvider.java
Sat May  3 22:10:58 2014
@@ -42,12 +42,12 @@ public abstract class AbstractLoggerProv
     /**
      * Marker for method entry tracing.
      */
-    public static final Marker ENTRY_MARKER = MarkerManager.getMarker("ENTRY").add(FLOW_MARKER);
+    public static final Marker ENTRY_MARKER = MarkerManager.getMarker("ENTRY").setParents(FLOW_MARKER);
 
     /**
      * Marker for method exit tracing.
      */
-    public static final Marker EXIT_MARKER = MarkerManager.getMarker("EXIT").add(FLOW_MARKER);
+    public static final Marker EXIT_MARKER = MarkerManager.getMarker("EXIT").setParents(FLOW_MARKER);
 
     /**
      * Marker for exception tracing.
@@ -57,12 +57,12 @@ public abstract class AbstractLoggerProv
     /**
      * Marker for throwing exceptions.
      */
-    public static final Marker THROWING_MARKER = MarkerManager.getMarker("THROWING").add(EXCEPTION_MARKER);
+    public static final Marker THROWING_MARKER = MarkerManager.getMarker("THROWING").setParents(EXCEPTION_MARKER);
 
     /**
      * Marker for catching exceptions.
      */
-    public static final Marker CATCHING_MARKER = MarkerManager.getMarker("CATCHING").add(EXCEPTION_MARKER);
+    public static final Marker CATCHING_MARKER = MarkerManager.getMarker("CATCHING").setParents(EXCEPTION_MARKER);
 
     /**
      * The default MessageFactory class.

Added: logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/MarkerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/MarkerTest.java?rev=1592286&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/MarkerTest.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/MarkerTest.java
Sat May  3 22:10:58 2014
@@ -0,0 +1,74 @@
+/*
+ * 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.log4j;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class MarkerTest {
+
+    @Test
+    public void markerTest() {
+        final Marker parent = MarkerManager.getMarker("PARENT");
+        final Marker test1 = MarkerManager.getMarker("TEST1").setParents(parent);
+        final Marker test2 = MarkerManager.getMarker("TEST2").addParents(parent);
+        assertTrue("TEST1 is not an instance of PARENT", test1.isInstanceOf(parent));
+        assertTrue("TEST2 is not an instance of PARENT", test2.isInstanceOf(parent));
+    }
+
+    @Test
+    public void multipleParentsTest() {
+        final Marker parent1 = MarkerManager.getMarker("PARENT1");
+        final Marker parent2 = MarkerManager.getMarker("PARENT2");
+        final Marker test1 = MarkerManager.getMarker("TEST1").setParents(parent1, parent2);
+        final Marker test2 = MarkerManager.getMarker("TEST2").addParents(parent1, parent2);
+        assertTrue("TEST1 is not an instance of PARENT1", test1.isInstanceOf(parent1));
+        assertTrue("TEST1 is not an instance of PARENT2", test1.isInstanceOf(parent2));
+        assertTrue("TEST2 is not an instance of PARENT1", test2.isInstanceOf(parent1));
+        assertTrue("TEST2 is not an instance of PARENT2", test2.isInstanceOf(parent2));
+    }
+
+    @Test
+    public void addToExistingParentsTest() {
+        final Marker parent = MarkerManager.getMarker("PARENT");
+        final Marker existing = MarkerManager.getMarker("EXISTING");
+        final Marker test1 = MarkerManager.getMarker("TEST1").setParents(existing);
+        test1.addParents(parent);
+        assertTrue("TEST1 is not an instance of PARENT", test1.isInstanceOf(parent));
+        assertTrue("TEST1 is not an instance of EXISTING", test1.isInstanceOf(existing));
+    }
+
+
+    @Test
+    public void duplicateParentsTest() {
+        final Marker parent = MarkerManager.getMarker("PARENT");
+        final Marker existing = MarkerManager.getMarker("EXISTING");
+        final Marker test1 = MarkerManager.getMarker("TEST1").setParents(existing);
+        test1.addParents(parent);
+        Marker[] parents = test1.getParents();
+        test1.addParents(existing);
+        assertTrue("duplicate add allowed", parents.length == test1.getParents().length);
+        test1.addParents(existing, MarkerManager.getMarker("EXTRA"));
+        assertTrue("incorrect add", parents.length + 1 == test1.getParents().length);
+        assertTrue("TEST1 is not an instance of PARENT", test1.isInstanceOf(parent));
+        assertTrue("TEST1 is not an instance of EXISTING", test1.isInstanceOf(existing));
+    }
+}

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverter.java?rev=1592286&r1=1592285&r2=1592286&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverter.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverter.java
Sat May  3 22:10:58 2014
@@ -34,24 +34,7 @@ public class MarkerAttributeConverter im
         if (marker == null) {
             return null;
         }
-
-        final StringBuilder builder = new StringBuilder(marker.getName());
-        Marker parent = marker.getParent();
-        int levels = 0;
-        boolean hasParent = false;
-        while (parent != null) {
-            levels++;
-            hasParent = true;
-            builder.append("[ ").append(parent.getName());
-            parent = parent.getParent();
-        }
-        for (int i = 0; i < levels; i++) {
-            builder.append(" ]");
-        }
-        if (hasParent) {
-            builder.append(" ]");
-        }
-        return builder.toString();
+        return marker.toString();
     }
 
     @Override

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java?rev=1592286&r1=1592285&r2=1592286&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
Sat May  3 22:10:58 2014
@@ -204,9 +204,17 @@ public class XMLLayout extends AbstractS
                 buf.append(this.namespacePrefix);
             }
             buf.append("Marker");
-            final Marker parent = marker.getParent();
-            if (parent != null) {
-                buf.append(" parent=\"").append(Transform.escapeHtmlTags(parent.getName())).append('"');
+            final Marker[] parents = marker.getParents();
+            if (parents != null && parents.length > 0) {
+                buf.append(" parents=\"");
+                boolean first = true;
+                for (Marker parent : parents) {
+                    if (!first) {
+                        buf.append(", ");
+                    }
+                    buf.append(parent.getName());
+                }
+                buf.append("\"");
             }
             buf.append('>');
             buf.append(Transform.escapeHtmlTags(marker.getName()));

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverterTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverterTest.java?rev=1592286&r1=1592285&r2=1592286&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverterTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/MarkerAttributeConverterTest.java
Sat May  3 22:10:58 2014
@@ -54,26 +54,26 @@ public class MarkerAttributeConverterTes
 
     @Test
     public void testConvert02() {
-        final Marker marker = MarkerManager.getMarker("testConvert02",
-                MarkerManager.getMarker("anotherConvert02",
-                        MarkerManager.getMarker("finalConvert03")
-                )
-        );
+        Marker marker = MarkerManager.getMarker("anotherConvert02").setParents(MarkerManager.getMarker("finalConvert03"));
+        marker = MarkerManager.getMarker("testConvert02").setParents(marker);
 
         final String converted = this.converter.convertToDatabaseColumn(marker);
 
         assertNotNull("The converted value should not be null.", converted);
-        assertEquals("The converted value is not correct.", "testConvert02[ anotherConvert02[
finalConvert03 ] ] ]",
+        assertEquals("The converted value is not correct.", "testConvert02[ anotherConvert02[
finalConvert03 ] ]",
                 converted);
 
         final Marker reversed = this.converter.convertToEntityAttribute(converted);
 
         assertNotNull("The reversed value should not be null.", reversed);
         assertEquals("The reversed value is not correct.", "testConvert02", marker.getName());
-        assertNotNull("The first parent should not be null.", marker.getParent());
-        assertEquals("The first parent is not correct.", "anotherConvert02", marker.getParent().getName());
-        assertNotNull("The second parent should not be null.", marker.getParent().getParent());
-        assertEquals("The second parent is not correct.", "finalConvert03", marker.getParent().getParent().getName());
+        Marker[] parents = marker.getParents();
+        assertNotNull("The first parent should not be null.", parents);
+        assertNotNull("The first parent should not be null.", parents[0]);
+        assertEquals("The first parent is not correct.", "anotherConvert02", parents[0].getName());
+        assertNotNull("The second parent should not be null.", parents[0].getParents());
+        assertNotNull("The second parent should not be null.", parents[0].getParents()[0]);
+        assertEquals("The second parent is not correct.", "finalConvert03", parents[0].getParents()[0].getName());
     }
 
     @Test

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=1592286&r1=1592285&r2=1592286&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
Sat May  3 22:10:58 2014
@@ -35,9 +35,9 @@ public class MarkerFilterTest {
     @Test
     public void testMarkers() {
         final Marker parent = MarkerManager.getMarker("Parent");
-        final Marker child = MarkerManager.getMarker("Child", parent);
-        final Marker grandChild = MarkerManager.getMarker("GrandChild", child);
-        final Marker sibling = MarkerManager.getMarker("Sibling", parent);
+        final Marker child = MarkerManager.getMarker("Child").setParents(parent);
+        final Marker grandChild = MarkerManager.getMarker("GrandChild").setParents(child);
+        final Marker sibling = MarkerManager.getMarker("Sibling").setParents(parent);
         final Marker stranger = MarkerManager.getMarker("Stranger");
         MarkerFilter filter = MarkerFilter.createFilter("Parent", null, null);
         filter.start();

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java?rev=1592286&r1=1592285&r2=1592286&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java
Sat May  3 22:10:58 2014
@@ -37,7 +37,7 @@ public class MarkerPatternConverterTest 
     public void testLookup() {
         final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit");
         final Marker eventMarker = MarkerManager.getMarker("EVENT");
-        final Marker auditMarker = MarkerManager.getMarker("AUDIT", eventMarker);
+        final Marker auditMarker = MarkerManager.getMarker("AUDIT").setParents(eventMarker);
         final LogEvent event = new Log4jLogEvent("MyLogger", auditMarker, null, Level.DEBUG,
msg, null);
         final StringBuilder sb = new StringBuilder();
         final MarkerPatternConverter converter = MarkerPatternConverter.newInstance(null);

Modified: 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=1592286&r1=1592285&r2=1592286&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java
Sat May  3 22:10:58 2014
@@ -47,7 +47,7 @@ public class Log4jMarker implements Mark
     @Override
     public void add(Marker marker) {
         Marker m = factory.getMarker(marker.getName());
-        this.marker.add(((Log4jMarker)m).getLog4jMarker());
+        this.marker.addParents(((Log4jMarker)m).getLog4jMarker());
     }
 
     @Override



Mime
View raw message