1
2
3
4
5
6
7
8
9
10
11
12
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
25
26
27
28
29
30 public class DuplicateMessageFilter extends TurboFilter {
31
32
33
34
35 public static final int DEFAULT_CACHE_SIZE = 100;
36
37
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
75
76
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 }