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>Both Key and the DefaultValue are user specified properties.
027 *
028 * @author Ceki G&uuml;lc&uuml;
029 */
030public class MDCBasedDiscriminator extends AbstractDiscriminator<ILoggingEvent> {
031
032    private String key;
033    private String defaultValue;
034
035    /**
036     * Return the value associated with an MDC entry designated by the Key
037     * property. If that value is null, then return the value assigned to the
038     * DefaultValue property.
039     */
040    public String getDiscriminatingValue(ILoggingEvent event) {
041        // http://jira.qos.ch/browse/LBCLASSIC-213
042        Map<String, String> mdcMap = event.getMDCPropertyMap();
043        if (mdcMap == null) {
044            return defaultValue;
045        }
046        String mdcValue = mdcMap.get(key);
047        if (mdcValue == null) {
048            return defaultValue;
049        } else {
050            return mdcValue;
051        }
052    }
053
054    @Override
055    public void start() {
056        int errors = 0;
057        if (OptionHelper.isEmpty(key)) {
058            errors++;
059            addError("The \"Key\" property must be set");
060        }
061        if (OptionHelper.isEmpty(defaultValue)) {
062            errors++;
063            addError("The \"DefaultValue\" property must be set");
064        }
065        if (errors == 0) {
066            started = true;
067        }
068    }
069
070    public String getKey() {
071        return key;
072    }
073
074    public void setKey(String key) {
075        this.key = key;
076    }
077
078    /**
079     * @return
080     * @see #setDefaultValue(String)
081     */
082    public String getDefaultValue() {
083        return defaultValue;
084    }
085
086    /**
087     * The default MDC value in case the MDC is not set for
088     * {@link #setKey(String) mdcKey}.
089     * <p/>
090     * <p> For example, if {@link #setKey(String) Key} is set to the value
091     * "someKey", and the MDC is not set for "someKey", then this appender will
092     * use the default value, which you can set with the help of this method.
093     *
094     * @param defaultValue
095     */
096    public void setDefaultValue(String defaultValue) {
097        this.defaultValue = defaultValue;
098    }
099}