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.filter;
018    
019    import org.apache.logging.log4j.Level;
020    import org.apache.logging.log4j.Marker;
021    import org.apache.logging.log4j.ThreadContext;
022    import org.apache.logging.log4j.core.LogEvent;
023    import org.apache.logging.log4j.core.Logger;
024    import org.apache.logging.log4j.core.config.plugins.Plugin;
025    import org.apache.logging.log4j.core.config.plugins.PluginAttr;
026    import org.apache.logging.log4j.core.config.plugins.PluginElement;
027    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
028    import org.apache.logging.log4j.message.Message;
029    
030    import java.util.HashMap;
031    import java.util.Map;
032    
033    /**
034     * Compare against a log level that is associated with an MDC value.
035     */
036    @Plugin(name = "DynamicThresholdFilter", type = "Core", elementType = "filter", printObject = true)
037    public final class DynamicThresholdFilter extends FilterBase {
038        private Map<String, Level> levelMap = new HashMap<String, Level>();
039        private Level defaultThreshold = Level.ERROR;
040        private String key;
041    
042        private DynamicThresholdFilter(String key, Map<String, Level> pairs, Level defaultLevel,
043                                       Result onMatch, Result onMismatch) {
044            super(onMatch, onMismatch);
045            if (key == null) {
046                throw new NullPointerException("key cannot be null");
047            }
048            this.key = key;
049            this.levelMap = pairs;
050            this.defaultThreshold = defaultLevel;
051        }
052    
053        public String getKey() {
054            return this.key;
055        }
056    
057        public Result filter(Logger logger, Level level, Marker marker, String msg, Object[] params) {
058            return filter(level);
059        }
060    
061        public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) {
062            return filter(level);
063        }
064    
065        public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
066            return filter(level);
067        }
068    
069        @Override
070        public Result filter(LogEvent event) {
071            return filter(event.getLevel());
072        }
073    
074        private Result filter(Level level) {
075            Object value = ThreadContext.get(key);
076            if (value != null) {
077                Level ctxLevel = levelMap.get(value);
078                if (ctxLevel == null) {
079                    ctxLevel = defaultThreshold;
080                }
081                return level.isAtLeastAsSpecificAs(ctxLevel) ? onMatch : onMismatch;
082            }
083            return Result.NEUTRAL;
084    
085        }
086    
087        @Override
088        public String toString() {
089            StringBuilder sb = new StringBuilder();
090            sb.append("key=").append(key);
091            sb.append(", default=").append(defaultThreshold);
092            if (levelMap.size() > 0) {
093                sb.append("{");
094                boolean first = true;
095                for (Map.Entry<String, Level> entry : levelMap.entrySet()) {
096                    if (!first) {
097                        sb.append(", ");
098                        first = false;
099                    }
100                    sb.append(entry.getKey()).append("=").append(entry.getValue());
101                }
102                sb.append("}");
103            }
104            return sb.toString();
105        }
106    
107        /**
108         * Create the DynamicThresholdFilter.
109         * @param key The name of the key to compare.
110         * @param pairs An array of value and Level pairs.
111         * @param level The default Level.
112         * @param match The action to perform if a match occurs.
113         * @param mismatch The action to perform if no match occurs.
114         * @return The DynamicThresholdFilter.
115         */
116        @PluginFactory
117        public static DynamicThresholdFilter createFilter(@PluginAttr("key") String key,
118                                                          @PluginElement("pairs") ValueLevelPair[] pairs,
119                                                          @PluginAttr("defaultThreshold") String level,
120                                                          @PluginAttr("onmatch") String match,
121                                                          @PluginAttr("onmismatch") String mismatch) {
122            Result onMatch = match == null ? null : Result.valueOf(match.toUpperCase());
123            Result onMismatch = mismatch == null ? null : Result.valueOf(mismatch.toUpperCase());
124            Map<String, Level> map = new HashMap<String, Level>();
125            for (ValueLevelPair pair : pairs) {
126                map.put(pair.getKey(), pair.getLevel());
127            }
128            Level l = Level.toLevel(level, Level.ERROR);
129            return new DynamicThresholdFilter(key, map, l, onMatch, onMismatch);
130        }
131    }