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  
14  import ch.qos.logback.classic.Level;
15  import ch.qos.logback.classic.Logger;
16  import ch.qos.logback.core.spi.FilterReply;
17  
18  /**
19   * 
20   * See {@link http://logback.qos.ch/manual/filters.html#DuplicateMessageFilter}
21   * for details.
22   * 
23   * @author Ceki Gulcu
24   * 
25   */
26  public class DuplicateMessageFilter extends TurboFilter {
27  
28    /**
29     * The default cache size.
30     */
31    public static final int DEFAULT_CACHE_SIZE = 100;
32    /**
33     * The default number of allows repetitions.
34     */
35    public static final int DEFAULT_ALLOWED_REPETITIONS = 5;
36  
37    public int allowedRepetitions = DEFAULT_ALLOWED_REPETITIONS;
38    public int cacheSize = DEFAULT_CACHE_SIZE;
39  
40    private LRUMessageCache msgCache;
41  
42    @Override
43    public void start() {
44      msgCache = new LRUMessageCache(cacheSize);
45      super.start();
46    }
47  
48    @Override
49    public void stop() {
50      msgCache.clear();
51      msgCache = null;
52      super.stop();
53    }
54  
55    @Override
56    public FilterReply decide(Marker marker, Logger logger, Level level,
57        String format, Object[] params, Throwable t) {
58      int count = msgCache.getMessageCount(format);
59      if (count <= allowedRepetitions) {
60        return FilterReply.NEUTRAL;
61      } else {
62        return FilterReply.DENY;
63      }
64    }
65  
66    public int getAllowedRepetitions() {
67      return allowedRepetitions;
68    }
69  
70    /**
71     * The allowed number of repetitions before
72     * 
73     * @param allowedRepetitions
74     */
75    public void setAllowedRepetitions(int allowedRepetitions) {
76      this.allowedRepetitions = allowedRepetitions;
77    }
78  
79    public int getCacheSize() {
80      return cacheSize;
81    }
82  
83    public void setCacheSize(int cacheSize) {
84      this.cacheSize = cacheSize;
85    }
86  
87  }