View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.routing;
18  
19  import org.apache.logging.log4j.Logger;
20  import org.apache.logging.log4j.core.Core;
21  import org.apache.logging.log4j.core.config.Node;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
24  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
25  import org.apache.logging.log4j.core.config.plugins.PluginNode;
26  import org.apache.logging.log4j.status.StatusLogger;
27  
28  /**
29   * A Route to an appender.
30   */
31  @Plugin(name = "Route", category = Core.CATEGORY_NAME, printObject = true, deferChildren = true)
32  public final class Route {
33      private static final Logger LOGGER = StatusLogger.getLogger();
34  
35      private final Node node;
36      private final String appenderRef;
37      private final String key;
38  
39      private Route(final Node node, final String appenderRef, final String key) {
40          this.node = node;
41          this.appenderRef = appenderRef;
42          this.key = key;
43      }
44  
45      /**
46       * Returns the Dynamic Appender Node.
47       * @return The Node.
48       */
49      public Node getNode() {
50          return node;
51      }
52  
53      /**
54       * Returns the appender reference.
55       * @return The Appender reference.
56       */
57      public String getAppenderRef() {
58          return appenderRef;
59      }
60  
61      /**
62       * Returns the key for this Route.
63       * @return the key for this Route.
64       */
65      public String getKey() {
66          return key;
67      }
68  
69      @Override
70      public String toString() {
71          final StringBuilder sb = new StringBuilder("Route(");
72          sb.append("type=");
73          if (appenderRef != null) {
74              sb.append("static Reference=").append(appenderRef);
75          } else if (node != null) {
76              sb.append("dynamic - type=").append(node.getName());
77          } else {
78              sb.append("invalid Route");
79          }
80          if (key != null) {
81              sb.append(" key='").append(key).append('\'');
82          } else {
83              sb.append(" default");
84          }
85          sb.append(')');
86          return sb.toString();
87      }
88  
89      /**
90       * Create the Route.
91       * @param appenderRef The Appender reference.
92       * @param key The key.
93       * @param node The Node.
94       * @return A Route.
95       */
96      @PluginFactory
97      public static Route createRoute(
98              @PluginAttribute("ref") final String appenderRef,
99              @PluginAttribute("key") final String key,
100             @PluginNode final Node node) {
101         if (node != null && node.hasChildren()) {
102             if (appenderRef != null) {
103                 LOGGER.error("A route cannot be configured with an appender reference and an appender definition");
104                 return null;
105             }
106         } else {
107             if (appenderRef == null) {
108                 LOGGER.error("A route must specify an appender reference or an appender definition");
109                 return null;
110             }
111         }
112         return new Route(node, appenderRef, key);
113     }
114 }