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.Core; 021import org.apache.logging.log4j.core.config.Node; 022import org.apache.logging.log4j.core.config.plugins.Plugin; 023import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 024import org.apache.logging.log4j.core.config.plugins.PluginFactory; 025import org.apache.logging.log4j.core.config.plugins.PluginNode; 026import org.apache.logging.log4j.status.StatusLogger; 027 028/** 029 * A Route to an appender. 030 */ 031@Plugin(name = "Route", category = Core.CATEGORY_NAME, printObject = true, deferChildren = true) 032public final class Route { 033 private static final Logger LOGGER = StatusLogger.getLogger(); 034 035 private final Node node; 036 private final String appenderRef; 037 private final String key; 038 039 private Route(final Node node, final String appenderRef, final String key) { 040 this.node = node; 041 this.appenderRef = appenderRef; 042 this.key = key; 043 } 044 045 /** 046 * Returns the Dynamic Appender Node. 047 * @return The Node. 048 */ 049 public Node getNode() { 050 return node; 051 } 052 053 /** 054 * Returns the appender reference. 055 * @return The Appender reference. 056 */ 057 public String getAppenderRef() { 058 return appenderRef; 059 } 060 061 /** 062 * Returns the key for this Route. 063 * @return the key for this Route. 064 */ 065 public String getKey() { 066 return key; 067 } 068 069 @Override 070 public String toString() { 071 final StringBuilder sb = new StringBuilder("Route("); 072 sb.append("type="); 073 if (appenderRef != null) { 074 sb.append("static Reference=").append(appenderRef); 075 } else if (node != null) { 076 sb.append("dynamic - type=").append(node.getName()); 077 } else { 078 sb.append("invalid Route"); 079 } 080 if (key != null) { 081 sb.append(" key='").append(key).append('\''); 082 } else { 083 sb.append(" default"); 084 } 085 sb.append(')'); 086 return sb.toString(); 087 } 088 089 /** 090 * Create the Route. 091 * @param appenderRef The Appender reference. 092 * @param key The key. 093 * @param node The Node. 094 * @return A Route. 095 */ 096 @PluginFactory 097 public static Route createRoute( 098 @PluginAttribute("ref") final String appenderRef, 099 @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}