View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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 org.slf4j.MDC;
17  
18  import ch.qos.logback.classic.spi.ILoggingEvent;
19  import ch.qos.logback.core.sift.Discriminator;
20  import ch.qos.logback.core.spi.ContextAwareBase;
21  import ch.qos.logback.core.util.OptionHelper;
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   * 
27   * <p>Both Key and the DefaultValue are user specified properties.
28   * 
29   * @author Ceki G&uuml;lc&uuml;
30   * 
31   */
32  public class MDCBasedDiscriminator extends ContextAwareBase implements
33      Discriminator<ILoggingEvent> {
34  
35    private String key;
36    private String defaultValue;
37    private boolean started = false;
38  
39    public MDCBasedDiscriminator() {
40    }
41  
42    /**
43     * Return the value associated with an MDC entry designated by the Key
44     * property. If that value is null, then return the value assigned to the
45     * DefaultValue property.
46     */
47    public String getDiscriminatingValue(ILoggingEvent event) {
48      String mdcValue = MDC.get(key);
49      if (mdcValue == null) {
50        return defaultValue;
51      } else {
52        return mdcValue;
53      }
54    }
55  
56    public boolean isStarted() {
57      return started;
58    }
59  
60    public void start() {
61      int errors = 0;
62      if (OptionHelper.isEmpty(key)) {
63        errors++;
64        addError("The \"Key\" property must be set");
65      }
66      if (OptionHelper.isEmpty(defaultValue)) {
67        errors++;
68        addError("The \"DefaultValue\" property must be set");
69      }
70      if (errors == 0) {
71        started = true;
72      }
73    }
74  
75    public void stop() {
76      started = false;
77    }
78  
79    public String getKey() {
80      return key;
81    }
82  
83    public void setKey(String key) {
84      this.key = key;
85    }
86  
87    /**
88     * @see #setDefaultValue(String)
89     * @return
90     */
91    public String getDefaultValue() {
92      return defaultValue;
93    }
94  
95    /**
96     * The default MDC value in case the MDC is not set for
97     * {@link #setKey(String) mdcKey}.
98     * 
99     * <p> For example, if {@link #setKey(String) Key} is set to the value
100    * "someKey", and the MDC is not set for "someKey", then this appender will
101    * use the default value, which you can set with the help of this method.
102    * 
103    * @param defaultValue
104    */
105   public void setDefaultValue(String defaultValue) {
106     this.defaultValue = defaultValue;
107   }
108 }