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.turbo;
11  
12  import org.slf4j.MDC;
13  import org.slf4j.Marker;
14  
15  import ch.qos.logback.classic.Level;
16  import ch.qos.logback.classic.Logger;
17  import ch.qos.logback.core.spi.FilterReply;
18  
19  /**
20   * This class allows output for a given MDC value.
21   * 
22   * <p>
23   * When the given value is identified by this TubroFilter, 
24   * the reply is based on the OnMatch option.
25   * The information is taken from the MDC. For this TurboFilter to work,
26   * one must set the key that will be used to 
27   * access the information in the MDC.
28   * 
29   * <p>
30   * To allow output for the value, set the OnMatch option
31   * to ACCEPT. To disable output for the given value, set
32   * the OnMatch option to DENY.
33   * 
34   * <p>
35   * By default, values of the OnMatch and OnMisMatch
36   * options are set to NEUTRAL.
37   * 
38   *
39   * @author Ceki G&uuml;lc&uuml;
40   * @author S&eacute;bastien Pennec
41   */
42  public class MDCFilter extends MatchingFilter {
43  
44    String MDCKey;
45    String value;
46    
47    @Override
48    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
49      if (MDCKey == null) {
50        return FilterReply.NEUTRAL;
51      }
52      
53      String value = MDC.get(MDCKey);
54      if (this.value.equals(value)) {
55        return onMatch;
56      }
57      return onMismatch;
58    }
59    
60    public void setValue(String value) {
61      this.value = value;
62    }
63    
64    public void setMDCKey(String MDCKey) {
65      this.MDCKey = MDCKey;
66    }
67  
68  }