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