001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.turbo;
015
016import java.util.ArrayList;
017import java.util.List;
018
019import org.slf4j.MDC;
020import org.slf4j.Marker;
021
022import ch.qos.logback.classic.ClassicConstants;
023import ch.qos.logback.classic.Level;
024import ch.qos.logback.classic.Logger;
025import ch.qos.logback.core.spi.FilterReply;
026
027/**
028 * This class allows output of debug level events to a certain list of users.
029 * 
030 * If the level passed as a parameter is of level DEBUG, then the "user" value
031 * taken from the MDC is checked against the configured user list. When the user
032 * belongs to the list, the request is accepted. Otherwise a NEUTRAL response
033 * is sent, thus not influencing the filter chain.  
034 *
035 * @author Ceki Gülcü
036 * @author Sébastien Pennec
037 */
038public class DebugUsersTurboFilter extends TurboFilter {
039
040    List<String> userList = new ArrayList<String>();
041
042    @Override
043    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
044        if (!level.equals(Level.DEBUG)) {
045            return FilterReply.NEUTRAL;
046        }
047        String user = MDC.get(ClassicConstants.USER_MDC_KEY);
048        if (user != null && userList.contains(user)) {
049            return FilterReply.ACCEPT;
050        }
051        return FilterReply.NEUTRAL;
052    }
053
054    public void addUser(String user) {
055        userList.add(user);
056    }
057
058    // test in BasicJoranTest only, to be removed asap.
059    public List<String> getUsers() {
060        return userList;
061    }
062
063}