View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.turbo;
11  
12  import org.slf4j.Marker;
13  import org.slf4j.MarkerFactory;
14  
15  import ch.qos.logback.classic.Level;
16  import ch.qos.logback.classic.Logger;
17  import ch.qos.logback.core.spi.FilterReply;
18  
19  /**
20   * Checks whether the marker in the event matches the marker specified by the 
21   * user.
22   */
23  public class MarkerFilter extends MatchingFilter {
24  
25    Marker markerToMatch;
26  
27    @Override
28    public void start() {
29      if(markerToMatch != null) {
30        super.start();
31      } else {
32        addError("The marker property must be set for ["+getName()+"]");
33      }
34    }
35    
36    @Override
37    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
38      if(!isStarted()) {
39        return FilterReply.NEUTRAL;
40      }
41      
42      if(marker == null) {
43        return onMismatch;
44      } 
45      
46      if(markerToMatch.contains(marker)) {
47        return onMatch;
48      } else {
49        return onMismatch;
50      }
51    }
52  
53    /**
54     * The marker to match in the event.
55     * 
56     * @param markerToMatch
57     */
58    public void setMarker(String markerStr) {
59      if(markerStr != null) {
60        this.markerToMatch = MarkerFactory.getMarker(markerStr);
61      }
62    }
63  }