logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1416067 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/appender/routing/ core/src/main/java/org/apache/logging/log4j/core/config/ core/src/test/java/org/apache/logging/log4j/core/appender/routing/ core/s...
Date Sat, 01 Dec 2012 22:43:29 GMT
Author: rgoers
Date: Sat Dec  1 22:43:28 2012
New Revision: 1416067

URL: http://svn.apache.org/viewvc?rev=1416067&view=rev
Log:
LOG4J2-129 - RoutingAppender was only creating a single appender for the default Route.

Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/Route.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Node.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderTest.java
    logging/log4j/log4j2/trunk/core/src/test/resources/log4j-routing.xml
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/Route.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/Route.java?rev=1416067&r1=1416066&r2=1416067&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/Route.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/Route.java
Sat Dec  1 22:43:28 2012
@@ -71,8 +71,10 @@ public final class Route {
         sb.append("type=");
         if (appenderRef != null) {
             sb.append("static Reference=").append(appenderRef);
+        } else if (node != null) {
+            sb.append("dynamic - type=").append(node.getName());
         } else {
-            sb.append("dynamic");
+            sb.append("invalid Route");
         }
         if (key != null) {
             sb.append(" key='").append(key).append("'");

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java?rev=1416067&r1=1416066&r2=1416067&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java
Sat Dec  1 22:43:28 2012
@@ -46,6 +46,7 @@ import java.util.concurrent.ConcurrentMa
 public final class RoutingAppender extends AbstractAppender {
     private static final String DEFAULT_KEY = "ROUTING_APPENDER_DEFAULT";
     private final Routes routes;
+    private final Route defaultRoute;
     private final Configuration config;
     private ConcurrentMap<String, AppenderControl> appenders = new ConcurrentHashMap<String,
AppenderControl>();
     private final RewritePolicy rewritePolicy;
@@ -56,25 +57,29 @@ public final class RoutingAppender exten
         this.routes = routes;
         this.config = config;
         this.rewritePolicy = rewritePolicy;
+        Route defRoute = null;
+        for (Route route : routes.getRoutes()) {
+            if (route.getKey() == null) {
+                if (defRoute == null) {
+                    defRoute = route;
+                } else {
+                    error("Multiple default routes. Route " + route.toString() + " will be
ignored");
+                }
+            }
+        }
+        defaultRoute = defRoute;
     }
 
     @Override
     public void start() {
         Map<String, Appender> map = config.getAppenders();
+        // Register all the static routes.
         for (Route route : routes.getRoutes()) {
             if (route.getAppenderRef() != null) {
                 Appender appender = map.get(route.getAppenderRef());
                 if (appender != null) {
-                    String key = route.getKey() == null ? DEFAULT_KEY : route.getKey();
-                    if (appenders.containsKey(key)) {
-                        if (DEFAULT_KEY.equals(key)) {
-                            LOGGER.error("Multiple default routes. Only the first will be
used");
-                        } else {
-                            LOGGER.error("Duplicate route " + key + " is ignored");
-                        }
-                    } else {
-                        appenders.put(key, new AppenderControl(appender, null, null));
-                    }
+                    String key = route == defaultRoute ? DEFAULT_KEY : route.getKey();
+                    appenders.put(key, new AppenderControl(appender, null, null));
                 } else {
                     LOGGER.error("Appender " + route.getAppenderRef() + " cannot be located.
Route ignored");
                 }
@@ -89,7 +94,7 @@ public final class RoutingAppender exten
         Map<String, Appender> map = config.getAppenders();
         for (Map.Entry<String, AppenderControl> entry : appenders.entrySet()) {
             String name = entry.getValue().getAppender().getName();
-            if (!DEFAULT_KEY.equals(entry.getKey()) && !map.containsKey(name)) {
+            if (!map.containsKey(name)) {
                 entry.getValue().getAppender().stop();
             }
         }
@@ -108,7 +113,6 @@ public final class RoutingAppender exten
 
     private synchronized AppenderControl getControl(String key, LogEvent event) {
         AppenderControl control = appenders.get(key);
-        boolean defaultRoute = false;
         if (control != null) {
             return control;
         }
@@ -120,17 +124,7 @@ public final class RoutingAppender exten
             }
         }
         if (route == null) {
-            control = appenders.get(DEFAULT_KEY);
-            if (control != null) {
-                return control;
-            }
-            for (Route r : routes.getRoutes()) {
-                if (r.getAppenderRef() == null && r.getKey() == null) {
-                    route = r;
-                    defaultRoute = true;
-                    break;
-                }
-            }
+            route = defaultRoute;
         }
         if (route != null) {
             Appender app = createAppender(route, event);
@@ -139,9 +133,6 @@ public final class RoutingAppender exten
             }
             control = new AppenderControl(app, null, null);
             appenders.put(key, control);
-            if (defaultRoute) {
-                appenders.put(DEFAULT_KEY, control);
-            }
         }
 
         return control;
@@ -151,11 +142,12 @@ public final class RoutingAppender exten
         Node routeNode = route.getNode();
         for (Node node : routeNode.getChildren()) {
             if (node.getType().getElementName().equals("appender")) {
-                config.createConfiguration(node, event);
-                if (node.getObject() instanceof Appender) {
-                    Appender app = (Appender) node.getObject();
+                Node appNode = new Node(node);
+                config.createConfiguration(appNode, event);
+                if (appNode.getObject() instanceof Appender) {
+                    Appender app = (Appender) appNode.getObject();
                     app.start();
-                    return (Appender) node.getObject();
+                    return app;
                 }
                 LOGGER.error("Unable to create Appender of type " + node.getName());
                 return null;

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Node.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Node.java?rev=1416067&r1=1416066&r2=1416067&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Node.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Node.java
Sat Dec  1 22:43:28 2012
@@ -57,6 +57,16 @@ public class Node {
         this.type = null;
     }
 
+    public Node(Node node) {
+        this.parent = node.parent;
+        this.name = node.name;
+        this.type = node.type;
+        this.attributes.putAll(node.getAttributes());
+        this.value = node.getValue();
+        this.children.addAll(node.getChildren());
+        this.object = node.object;
+    }
+
     public Map<String, String> getAttributes() {
         return attributes;
     }

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderTest.java?rev=1416067&r1=1416066&r2=1416067&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderTest.java
Sat Dec  1 22:43:28 2012
@@ -77,9 +77,13 @@ public class RoutingAppenderTest {
         List<LogEvent> list = app.getEvents();
         assertNotNull("No events generated", list);
         assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size()
== 1);
-        msg = new StructuredDataMessage("Test", "This is a test", "Unknown");
+        msg = new StructuredDataMessage("Test", "This is a test", "Alert");
         EventLogger.logEvent(msg);
-        File file = new File("target/rolling1/rollingtest-Unknown.log");
-        assertTrue("File was not created", file.exists());
+        File file = new File("target/routing1/routingtest-Alert.log");
+        assertTrue("Alert file was not created", file.exists());
+        msg = new StructuredDataMessage("Test", "This is a test", "Activity");
+        EventLogger.logEvent(msg);
+        file = new File("target/routing1/routingtest-Activity.log");
+        assertTrue("Activity file was not created", file.exists());
     }
 }

Modified: logging/log4j/log4j2/trunk/core/src/test/resources/log4j-routing.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/log4j-routing.xml?rev=1416067&r1=1416066&r2=1416067&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/log4j-routing.xml (original)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/log4j-routing.xml Sat Dec  1 22:43:28
2012
@@ -18,7 +18,7 @@
 -->
 <configuration status="debug" name="RoutingTest" packages="org.apache.logging.log4j.test">
   <properties>
-    <property name="filename">target/rolling1/rollingtest-$${sd:type}.log</property>
+    <property name="filename">target/routing1/routingtest-$${sd:type}.log</property>
   </properties>
   <ThresholdFilter level="debug"/>
 
@@ -32,8 +32,8 @@
     <Routing name="Routing">
       <Routes pattern="$${sd:type}">
         <Route>
-          <RollingFile name="Rolling-${sd:type}" fileName="${filename}"
-                       filePattern="target/rolling1/test1-${sd:type}.%i.log.gz">
+          <RollingFile name="Routing-${sd:type}" fileName="${filename}"
+                       filePattern="target/routing1/test1-${sd:type}.%i.log.gz">
             <PatternLayout>
               <pattern>%d %p %C{1.} [%t] %m%n</pattern>
             </PatternLayout>

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1416067&r1=1416066&r2=1416067&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sat Dec  1 22:43:28 2012
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta4" date="TBD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-129" dev="rgoers" type="fix">
+        RoutingAppender was only creating a single appender for the default Route.
+      </action>
       <action issue="LOG4J2-126" dev="rgoers" type="fix">
         Allow JMS appenders to recover if the queue or topic is unavailable.
       </action>



Mime
View raw message