View Javadoc

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