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     */
017    package org.apache.logging.log4j.core.config;
018    
019    import org.apache.logging.log4j.Level;
020    import org.apache.logging.log4j.Logger;
021    import org.apache.logging.log4j.core.Filter;
022    import org.apache.logging.log4j.core.config.plugins.Plugin;
023    import org.apache.logging.log4j.core.config.plugins.PluginAliases;
024    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
025    import org.apache.logging.log4j.core.config.plugins.PluginElement;
026    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
027    import org.apache.logging.log4j.status.StatusLogger;
028    
029    /**
030     * An Appender reference.
031     */
032    @Plugin(name = "AppenderRef", category = Node.CATEGORY, printObject = true)
033    @PluginAliases("appender-ref")
034    public final class AppenderRef {
035        private static final Logger LOGGER = StatusLogger.getLogger();
036    
037        private final String ref;
038        private final Level level;
039        private final Filter filter;
040    
041        private AppenderRef(final String ref, final Level level, final Filter filter) {
042            this.ref = ref;
043            this.level = level;
044            this.filter = filter;
045        }
046    
047        public String getRef() {
048            return ref;
049        }
050    
051        public Level getLevel() {
052            return level;
053        }
054    
055        public Filter getFilter() {
056            return filter;
057        }
058    
059        @Override
060        public String toString() {
061            return ref;
062        }
063    
064        /**
065         * Create an Appender reference.
066         * @param ref The name of the Appender.
067         * @param level The Level to filter against.
068         * @param filter The filter(s) to use.
069         * @return The name of the Appender.
070         */
071        @PluginFactory
072        public static AppenderRef createAppenderRef(
073                @PluginAttribute("ref") final String ref,
074                @PluginAttribute("level") final Level level,
075                @PluginElement("Filter") final Filter filter) {
076    
077            if (ref == null) {
078                LOGGER.error("Appender references must contain a reference");
079                return null;
080            }
081            return new AppenderRef(ref, level, filter);
082        }
083    }