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.plugins.Plugin;
    21  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
    22  import org.apache.logging.log4j.core.config.plugins.PluginElement;
    23  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
    24  import org.apache.logging.log4j.status.StatusLogger;
    25
    26  /**
    27   * Used to contain the individual Route elements.
    28   */
    29  @Plugin(name = "Routes", category = "Core", printObject = true)
    30  public final class Routes {
    31
    32      private static final Logger LOGGER = StatusLogger.getLogger();
    33
    34      private final String pattern;
    35      private final Route[] routes;
    36
    37      private Routes(final String pattern, final Route... routes) {
    38          this.pattern = pattern;
    39          this.routes = routes;
    40      }
    41
    42      /**
    43       * Returns the pattern.
    44       * @return the pattern.
    45       */
    46      public String getPattern() {
    47          return pattern;
    48      }
    49
    50      /**
    51       * Returns the array of Route elements.
    52       * @return an array of Route elements.
    53       */
    54      public Route[] getRoutes() {
    55          return routes;
    56      }
    57
    58      @Override
    59      public String toString() {
    60          final StringBuilder sb = new StringBuilder("{");
    61          boolean first = true;
    62          for (final Route route : routes) {
    63              if (!first) {
    64                  sb.append(',');
    65              }
    66              first = false;
    67              sb.append(route.toString());
    68          }
    69          sb.append('}');
    70          return sb.toString();
    71
    72      }
    73
    74      /**
    75       * Create the Routes.
    76       * @param pattern The pattern.
    77       * @param routes An array of Route elements.
    78       * @return The Routes container.
    79       */
    80      @PluginFactory
    81      public static Routes createRoutes(
    82              @PluginAttribute("pattern") final String pattern,
    83              @PluginElement("Routes") final Route... routes) {
    84          if (pattern == null) {
    85              LOGGER.error("A pattern is required");
    86              return null;
    87          }
    88          if (routes == null || routes.length == 0) {
    89              LOGGER.error("No routes configured");
    90              return null;
    91          }
    92          return new Routes(pattern, routes);
    93      }
    94  }