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  
19  /**
20   * This discriminator returns the value context to which this event is attached
21   * to. If the said value is null, then a default value is returned.
22   * 
23   * <p>
24   * Both Key and the DefaultValue are user specified properties.
25   * 
26   * @author Ceki G&uuml;lc&uuml;
27   * 
28   */
29  public class ContextBasedDiscriminator extends AbstractDiscriminator<ILoggingEvent> {
30  
31      private static final String KEY = "contextName";
32      private String defaultValue;
33  
34      /**
35       * Return the name of the current context name as found in the logging event.
36       */
37      public String getDiscriminatingValue(ILoggingEvent event) {
38          String contextName = event.getLoggerContextVO().getName();
39  
40          if (contextName == null) {
41              return defaultValue;
42          } else {
43              return contextName;
44          }
45      }
46  
47      public String getKey() {
48          return KEY;
49      }
50  
51      public void setKey(String key) {
52          throw new UnsupportedOperationException("Key cannot be set. Using fixed key " + KEY);
53      }
54  
55      /**
56       * @see #setDefaultValue(String)
57       * @return
58       */
59      public String getDefaultValue() {
60          return defaultValue;
61      }
62  
63      /**
64       * The default context name in case the context name is not set for the current
65       * logging event.
66       * 
67       * @param defaultValue
68       */
69      public void setDefaultValue(String defaultValue) {
70          this.defaultValue = defaultValue;
71      }
72  }