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 java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    
025    import org.apache.logging.log4j.Level;
026    import org.apache.logging.log4j.Marker;
027    import org.apache.logging.log4j.ThreadContext;
028    import org.apache.logging.log4j.core.LogEvent;
029    import org.apache.logging.log4j.core.Logger;
030    import org.apache.logging.log4j.core.config.plugins.Plugin;
031    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
032    import org.apache.logging.log4j.core.config.plugins.PluginElement;
033    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
034    import org.apache.logging.log4j.core.util.KeyValuePair;
035    import org.apache.logging.log4j.message.Message;
036    
037    /**
038     * Filter based on a value in the Thread Context Map (MDC).
039     */
040    @Plugin(name = "ThreadContextMapFilter", category = "Core", elementType = "filter", printObject = true)
041    public class ThreadContextMapFilter extends MapFilter {
042    
043        private final String key;
044        private final String value;
045    
046        private final boolean useMap;
047    
048        public ThreadContextMapFilter(final Map<String, List<String>> pairs, final boolean oper, final Result onMatch,
049                                      final Result onMismatch) {
050            super(pairs, oper, onMatch, onMismatch);
051            if (pairs.size() == 1) {
052                final Iterator<Map.Entry<String, List<String>>> iter = pairs.entrySet().iterator();
053                final Map.Entry<String, List<String>> entry = iter.next();
054                if (entry.getValue().size() == 1) {
055                    this.key = entry.getKey();
056                    this.value = entry.getValue().get(0);
057                    this.useMap = false;
058                } else {
059                    this.key = null;
060                    this.value = null;
061                    this.useMap = true;
062                }
063            } else {
064                this.key = null;
065                this.value = null;
066                this.useMap = true;
067            }
068        }
069    
070        @Override
071        public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
072                             final Object... params) {
073            return filter();
074        }
075    
076        @Override
077        public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
078                             final Throwable t) {
079            return filter();
080        }
081    
082        @Override
083        public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
084                             final Throwable t) {
085            return filter();
086        }
087    
088        private Result filter() {
089            boolean match = false;
090            if (useMap) {
091                for (final Map.Entry<String, List<String>> entry : getMap().entrySet()) {
092                    final String toMatch = ThreadContext.get(entry.getKey());
093                    if (toMatch != null) {
094                        match = entry.getValue().contains(toMatch);
095                    } else {
096                        match = false;
097                    }
098                    if ((!isAnd() && match) || (isAnd() && !match)) {
099                        break;
100                    }
101                }
102            } else {
103                match = value.equals(ThreadContext.get(key));
104            }
105            return match ? onMatch : onMismatch;
106        }
107    
108        @Override
109        public Result filter(final LogEvent event) {
110            return super.filter(event.getContextMap()) ? onMatch : onMismatch;
111        }
112    
113        @PluginFactory
114        public static ThreadContextMapFilter createFilter(
115                @PluginElement("Pairs") final KeyValuePair[] pairs,
116                @PluginAttribute("operator") final String oper,
117                @PluginAttribute("onMatch") final Result match,
118                @PluginAttribute("onMismatch") final Result mismatch) {
119            if (pairs == null || pairs.length == 0) {
120                LOGGER.error("key and value pairs must be specified for the ThreadContextMapFilter");
121                return null;
122            }
123            final Map<String, List<String>> map = new HashMap<String, List<String>>();
124            for (final KeyValuePair pair : pairs) {
125                final String key = pair.getKey();
126                if (key == null) {
127                    LOGGER.error("A null key is not valid in MapFilter");
128                    continue;
129                }
130                final String value = pair.getValue();
131                if (value == null) {
132                    LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
133                    continue;
134                }
135                List<String> list = map.get(pair.getKey());
136                if (list != null) {
137                    list.add(value);
138                } else {
139                    list = new ArrayList<String>();
140                    list.add(value);
141                    map.put(pair.getKey(), list);
142                }
143            }
144            if (map.isEmpty()) {
145                LOGGER.error("ThreadContextMapFilter is not configured with any valid key value pairs");
146                return null;
147            }
148            final boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
149            return new ThreadContextMapFilter(map, isAnd, match, mismatch);
150        }
151    }