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.turbo;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.slf4j.MDC;
20  import org.slf4j.Marker;
21  
22  import ch.qos.logback.classic.ClassicConstants;
23  import ch.qos.logback.classic.Level;
24  import ch.qos.logback.classic.Logger;
25  import ch.qos.logback.core.spi.FilterReply;
26  
27  /**
28   * This class allows output of debug level events to a certain list of users.
29   * 
30   * If the level passed as a parameter is of level DEBUG, then the "user" value
31   * taken from the MDC is checked against the configured user list. When the user
32   * belongs to the list, the request is accepted. Otherwise a NEUTRAL response is
33   * sent, thus not influencing the filter chain.
34   *
35   * @author Ceki Gülcü
36   * @author Sébastien Pennec
37   */
38  public class DebugUsersTurboFilter extends TurboFilter {
39  
40      List<String> userList = new ArrayList<String>();
41  
42      @Override
43      public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
44          if (!level.equals(Level.DEBUG)) {
45              return FilterReply.NEUTRAL;
46          }
47          String user = MDC.get(ClassicConstants.USER_MDC_KEY);
48          if (user != null && userList.contains(user)) {
49              return FilterReply.ACCEPT;
50          }
51          return FilterReply.NEUTRAL;
52      }
53  
54      public void addUser(String user) {
55          userList.add(user);
56      }
57  
58      // test in BasicJoranTest only, to be removed asap.
59      public List<String> getUsers() {
60          return userList;
61      }
62  
63  }