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.config.Node;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
23  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
24  import org.apache.logging.log4j.core.config.plugins.PluginNode;
25  import org.apache.logging.log4j.status.StatusLogger;
26  
27  /**
28   * A Route to an appender.
29   */
30  @Plugin(name = "Route", category = "Core", printObject = true, deferChildren = true)
31  public final class Route {
32      private static final Logger LOGGER = StatusLogger.getLogger();
33  
34      private final Node node;
35      private final String appenderRef;
36      private final String key;
37  
38      private Route(final Node node, final String appenderRef, final String key) {
39          this.node = node;
40          this.appenderRef = appenderRef;
41          this.key = key;
42      }
43  
44      /**
45       * Returns the Dynamic Appender Node.
46       * @return The Node.
47       */
48      public Node getNode() {
49          return node;
50      }
51  
52      /**
53       * Returns the appender reference.
54       * @return The Appender reference.
55       */
56      public String getAppenderRef() {
57          return appenderRef;
58      }
59  
60      /**
61       * Returns the key for this Route.
62       * @return the key for this Route.
63       */
64      public String getKey() {
65          return key;
66      }
67  
68      @Override
69      public String toString() {
70          final StringBuilder sb = new StringBuilder("Route(");
71          sb.append("type=");
72          if (appenderRef != null) {
73              sb.append("static Reference=").append(appenderRef);
74          } else if (node != null) {
75              sb.append("dynamic - type=").append(node.getName());
76          } else {
77              sb.append("invalid Route");
78          }
79          if (key != null) {
80              sb.append(" key='").append(key).append("'");
81          } else {
82              sb.append(" default");
83          }
84          sb.append(")");
85          return sb.toString();
86      }
87  
88      /**
89       * Create the Route.
90       * @param appenderRef The Appender reference.
91       * @param key The key.
92       * @param node The Node.
93       * @return A Route.
94       */
95      @PluginFactory
96      public static Route createRoute(
97              @PluginAttribute("ref") final String appenderRef,
98              @PluginAttribute("key") final String key,
99              @PluginNode final Node node) {
100         if (node != null && node.hasChildren()) {
101             if (appenderRef != null) {
102                 LOGGER.error("A route cannot be configured with an appender reference and an appender definition");
103                 return null;
104             }
105         } else {
106             if (appenderRef == null) {
107                 LOGGER.error("A route must specify an appender reference or an appender definition");
108                 return null;
109             }
110         }
111         return new Route(node, appenderRef, key);
112     }
113 }