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 org.slf4j.Marker;
017import org.slf4j.MarkerFactory;
018
019import ch.qos.logback.classic.Level;
020import ch.qos.logback.classic.Logger;
021import ch.qos.logback.core.spi.FilterReply;
022
023/**
024 * Checks whether the marker in the event matches the marker specified by the 
025 * user.
026 */
027public class MarkerFilter extends MatchingFilter {
028
029    Marker markerToMatch;
030
031    @Override
032    public void start() {
033        if (markerToMatch != null) {
034            super.start();
035        } else {
036            addError("The marker property must be set for [" + getName() + "]");
037        }
038    }
039
040    @Override
041    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
042        if (!isStarted()) {
043            return FilterReply.NEUTRAL;
044        }
045
046        if (marker == null) {
047            return onMismatch;
048        }
049
050        if (marker.contains(markerToMatch)) {
051            return onMatch;
052        } else {
053            return onMismatch;
054        }
055    }
056
057    /**
058     * The marker to match in the event.
059     * 
060     * @param markerStr
061     */
062    public void setMarker(String markerStr) {
063        if (markerStr != null) {
064            this.markerToMatch = MarkerFactory.getMarker(markerStr);
065        }
066    }
067}