1
2
3
4
5
6
7
8
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
21
22
23
24
25
26 public class DuplicateMessageFilter extends TurboFilter {
27
28
29
30
31 public static final int DEFAULT_CACHE_SIZE = 100;
32
33
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
72
73
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 }