    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.pattern;
    018
    019    import java.util.Map;
    020    import java.util.Set;
    021    import java.util.TreeSet;
    022
    023    import org.apache.logging.log4j.core.LogEvent;
    024    import org.apache.logging.log4j.core.config.plugins.Plugin;
    025
    026    /**
    027     * Able to handle the contents of the LogEvent's MDC and either
    028     * output the entire contents of the properties in a similar format to the
    029     * java.util.Hashtable.toString(), or to output the value of a specific key
    030     * within the property bundle
    031     * when this pattern converter has the option set.
    032     */
    033     @Plugin(name = "MdcPatternConverter", category = PatternConverter.CATEGORY)
    034    @ConverterKeys({ "X", "mdc", "MDC" })
    035    public final class MdcPatternConverter extends LogEventPatternConverter {
    036        /**
    037         * Name of property to output.
    038         */
    039        private final String key;
    040
    041        /**
    042         * Private constructor.
    043         *
    044         * @param options options, may be null.
    045         */
    046        private MdcPatternConverter(final String[] options) {
    047            super(options != null && options.length > 0 ? "MDC{" + options[0] + '}' : "MDC", "mdc");
    048            key = options != null && options.length > 0 ? options[0] : null;
    049        }
    050
    051        /**
    052         * Obtains an instance of PropertiesPatternConverter.
    053         *
    054         * @param options options, may be null or first element contains name of property to format.
    055         * @return instance of PropertiesPatternConverter.
    056         */
    057        public static MdcPatternConverter newInstance(final String[] options) {
    058            return new MdcPatternConverter(options);
    059        }
    060
    061        /**
    062         * {@inheritDoc}
    063         */
    064        @Override
    065        public void format(final LogEvent event, final StringBuilder toAppendTo) {
    066            final Map<String, String> contextMap = event.getContextMap();
    067            // if there is no additional options, we output every single
    068            // Key/Value pair for the MDC in a similar format to Hashtable.toString()
    069            if (key == null) {
    070
    071
    072                if (contextMap == null || contextMap.isEmpty()) {
    073                    toAppendTo.append("{}");
    074                    return;
    075                }
    076                final StringBuilder sb = new StringBuilder("{");
    077                final Set<String> keys = new TreeSet<String>(contextMap.keySet());
    078                for (final String key : keys) {
    079                    if (sb.length() > 1) {
    080                        sb.append(", ");
    081                    }
    082                    sb.append(key).append('=').append(contextMap.get(key));
    083
    084                }
    085                sb.append('}');
    086                toAppendTo.append(sb);
    087            } else if (contextMap != null) {
    088                // otherwise they just want a single key output
    089                final Object val = contextMap.get(key);
    090
    091                if (val != null) {
    092                    toAppendTo.append(val);
    093                }
    094            }
    095        }
    096    }