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