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