001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender.routing;
018
019import org.apache.logging.log4j.Logger;
020import org.apache.logging.log4j.core.config.Node;
021import org.apache.logging.log4j.core.config.plugins.Plugin;
022import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
023import org.apache.logging.log4j.core.config.plugins.PluginFactory;
024import org.apache.logging.log4j.core.config.plugins.PluginNode;
025import org.apache.logging.log4j.status.StatusLogger;
026
027/**
028 * A Route to an appender.
029 */
030@Plugin(name = "Route", category = "Core", printObject = true, deferChildren = true)
031public final class Route {
032    private static final Logger LOGGER = StatusLogger.getLogger();
033
034    private final Node node;
035    private final String appenderRef;
036    private final String key;
037
038    private Route(final Node node, final String appenderRef, final String key) {
039        this.node = node;
040        this.appenderRef = appenderRef;
041        this.key = key;
042    }
043
044    /**
045     * Returns the Dynamic Appender Node.
046     * @return The Node.
047     */
048    public Node getNode() {
049        return node;
050    }
051
052    /**
053     * Returns the appender reference.
054     * @return The Appender reference.
055     */
056    public String getAppenderRef() {
057        return appenderRef;
058    }
059
060    /**
061     * Returns the key for this Route.
062     * @return the key for this Route.
063     */
064    public String getKey() {
065        return key;
066    }
067
068    @Override
069    public String toString() {
070        final StringBuilder sb = new StringBuilder("Route(");
071        sb.append("type=");
072        if (appenderRef != null) {
073            sb.append("static Reference=").append(appenderRef);
074        } else if (node != null) {
075            sb.append("dynamic - type=").append(node.getName());
076        } else {
077            sb.append("invalid Route");
078        }
079        if (key != null) {
080            sb.append(" key='").append(key).append('\'');
081        } else {
082            sb.append(" default");
083        }
084        sb.append(')');
085        return sb.toString();
086    }
087
088    /**
089     * Create the Route.
090     * @param appenderRef The Appender reference.
091     * @param key The key.
092     * @param node The Node.
093     * @return A Route.
094     */
095    @PluginFactory
096    public static Route createRoute(
097            @PluginAttribute("ref") final String appenderRef,
098            @PluginAttribute("key") final String key,
099            @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}