View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.sift;
15  
16  import ch.qos.logback.classic.spi.ILoggingEvent;
17  import ch.qos.logback.core.sift.Discriminator;
18  import ch.qos.logback.core.spi.ContextAwareBase;
19  import ch.qos.logback.core.util.OptionHelper;
20  
21  import java.util.Map;
22  
23  /**
24   * MDCBasedDiscriminator essentially returns the value mapped to an MDC key. If
25   * the said value is null, then a default value is returned.
26   * <p/>
27   * <p>Both Key and the DefaultValue are user specified properties.
28   *
29   * @author Ceki G&uuml;lc&uuml;
30   */
31  public class MDCBasedDiscriminator extends ContextAwareBase implements
32          Discriminator<ILoggingEvent> {
33  
34    private String key;
35    private String defaultValue;
36    private boolean started = false;
37  
38    public MDCBasedDiscriminator() {
39    }
40  
41    /**
42     * Return the value associated with an MDC entry designated by the Key
43     * property. If that value is null, then return the value assigned to the
44     * DefaultValue property.
45     */
46    public String getDiscriminatingValue(ILoggingEvent event) {
47      // http://jira.qos.ch/browse/LBCLASSIC-213
48      Map<String, String> mdcMap = event.getMDCPropertyMap();
49      if (mdcMap == null) {
50        return defaultValue;
51      }
52      String mdcValue = mdcMap.get(key);
53      if (mdcValue == null) {
54        return defaultValue;
55      } else {
56        return mdcValue;
57      }
58    }
59  
60    public boolean isStarted() {
61      return started;
62    }
63  
64    public void start() {
65      int errors = 0;
66      if (OptionHelper.isEmpty(key)) {
67        errors++;
68        addError("The \"Key\" property must be set");
69      }
70      if (OptionHelper.isEmpty(defaultValue)) {
71        errors++;
72        addError("The \"DefaultValue\" property must be set");
73      }
74      if (errors == 0) {
75        started = true;
76      }
77    }
78  
79    public void stop() {
80      started = false;
81    }
82  
83    public String getKey() {
84      return key;
85    }
86  
87    public void setKey(String key) {
88      this.key = key;
89    }
90  
91    /**
92     * @return
93     * @see #setDefaultValue(String)
94     */
95    public String getDefaultValue() {
96      return defaultValue;
97    }
98  
99    /**
100    * The default MDC value in case the MDC is not set for
101    * {@link #setKey(String) mdcKey}.
102    * <p/>
103    * <p> For example, if {@link #setKey(String) Key} is set to the value
104    * "someKey", and the MDC is not set for "someKey", then this appender will
105    * use the default value, which you can set with the help of this method.
106    *
107    * @param defaultValue
108    */
109   public void setDefaultValue(String defaultValue) {
110     this.defaultValue = defaultValue;
111   }
112 }