001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.sift;
015
016import ch.qos.logback.classic.spi.ILoggingEvent;
017import ch.qos.logback.core.sift.AbstractDiscriminator;
018import ch.qos.logback.core.util.OptionHelper;
019
020import java.util.Map;
021
022/**
023 * MDCBasedDiscriminator essentially returns the value mapped to an MDC key. If
024 * the said value is null, then a default value is returned.
025 * <p/>
026 * <p>
027 * Both Key and the DefaultValue are user specified properties.
028 *
029 * @author Ceki G&uuml;lc&uuml;
030 */
031public class MDCBasedDiscriminator extends AbstractDiscriminator<ILoggingEvent> {
032
033    private String key;
034    private String defaultValue;
035
036    /**
037     * Return the value associated with an MDC entry designated by the Key property.
038     * If that value is null, then return the value assigned to the DefaultValue
039     * property.
040     */
041    public String getDiscriminatingValue(ILoggingEvent event) {
042        // http://jira.qos.ch/browse/LBCLASSIC-213
043        Map<String, String> mdcMap = event.getMDCPropertyMap();
044        if (mdcMap == null) {
045            return defaultValue;
046        }
047        String mdcValue = mdcMap.get(key);
048        if (mdcValue == null) {
049            return defaultValue;
050        } else {
051            return mdcValue;
052        }
053    }
054
055    @Override
056    public void start() {
057        int errors = 0;
058        if (OptionHelper.isNullOrEmptyOrAllSpaces(key)) {
059            errors++;
060            addError("The \"Key\" property must be set");
061        }
062        if (OptionHelper.isNullOrEmptyOrAllSpaces(defaultValue)) {
063            errors++;
064            addError("The \"DefaultValue\" property must be set");
065        }
066        if (errors == 0) {
067            started = true;
068        }
069    }
070
071    public String getKey() {
072        return key;
073    }
074
075    public void setKey(String key) {
076        this.key = key;
077    }
078
079    /**
080     * @return
081     * @see #setDefaultValue(String)
082     */
083    public String getDefaultValue() {
084        return defaultValue;
085    }
086
087    /**
088     * The default MDC value in case the MDC is not set for {@link #setKey(String)
089     * mdcKey}.
090     * <p/>
091     * <p>
092     * For example, if {@link #setKey(String) Key} is set to the value "someKey",
093     * and the MDC is not set for "someKey", then this appender will use the default
094     * value, which you can set with the help of this method.
095     *
096     * @param defaultValue
097     */
098    public void setDefaultValue(String defaultValue) {
099        this.defaultValue = defaultValue;
100    }
101}