View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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 org.slf4j.Marker;
17  import org.slf4j.MarkerFactory;
18  
19  import ch.qos.logback.classic.Level;
20  import ch.qos.logback.classic.Logger;
21  import ch.qos.logback.core.spi.FilterReply;
22  
23  /**
24   * Checks whether the marker in the event matches the marker specified by the 
25   * user.
26   */
27  public class MarkerFilter extends MatchingFilter {
28  
29    Marker markerToMatch;
30  
31    @Override
32    public void start() {
33      if(markerToMatch != null) {
34        super.start();
35      } else {
36        addError("The marker property must be set for ["+getName()+"]");
37      }
38    }
39    
40    @Override
41    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
42      if(!isStarted()) {
43        return FilterReply.NEUTRAL;
44      }
45      
46      if(marker == null) {
47        return onMismatch;
48      } 
49      
50      if(markerToMatch.contains(marker)) {
51        return onMatch;
52      } else {
53        return onMismatch;
54      }
55    }
56  
57    /**
58     * The marker to match in the event.
59     * 
60     * @param markerToMatch
61     */
62    public void setMarker(String markerStr) {
63      if(markerStr != null) {
64        this.markerToMatch = MarkerFactory.getMarker(markerStr);
65      }
66    }
67  }