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