1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.async;
18
19 import org.apache.logging.log4j.Level;
20 import org.apache.logging.log4j.Logger;
21 import org.apache.logging.log4j.status.StatusLogger;
22
23 import java.util.Objects;
24 import java.util.concurrent.atomic.AtomicLong;
25
26
27
28
29
30
31
32 public class DiscardingAsyncQueueFullPolicy extends DefaultAsyncQueueFullPolicy {
33 private static final Logger LOGGER = StatusLogger.getLogger();
34
35 private final Level thresholdLevel;
36 private final AtomicLong discardCount = new AtomicLong();
37
38
39
40
41
42
43
44 public DiscardingAsyncQueueFullPolicy(final Level thresholdLevel) {
45 this.thresholdLevel = Objects.requireNonNull(thresholdLevel, "thresholdLevel");
46 }
47
48 @Override
49 public EventRoute getRoute(final long backgroundThreadId, final Level level) {
50 if (level.isLessSpecificThan(thresholdLevel)) {
51 if (discardCount.getAndIncrement() == 0) {
52 LOGGER.warn("Async queue is full, discarding event with level {}. " +
53 "This message will only appear once; future events from {} " +
54 "are silently discarded until queue capacity becomes available.",
55 level, thresholdLevel);
56 }
57 return EventRoute.DISCARD;
58 }
59 return super.getRoute(backgroundThreadId, level);
60 }
61
62 public static long getDiscardCount(final AsyncQueueFullPolicy router) {
63 if (router instanceof DiscardingAsyncQueueFullPolicy) {
64 return ((DiscardingAsyncQueueFullPolicy) router).discardCount.get();
65 }
66 return 0;
67 }
68
69 public Level getThresholdLevel() {
70 return thresholdLevel;
71 }
72 }