1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
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 * Discarding router extends the DefaultAsyncQueueFullPolicy by first verifying if the queue is fuller than the specified
28 * threshold ratio; if this is the case, log events {@linkplain Level#isMoreSpecificThan(Level) more specific} than
29 * the specified threshold level are dropped. If this is not the case, the {@linkplain DefaultAsyncQueueFullPolicy
30 * default routing rules hold.
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 * Constructs a router that will discard events {@linkplain Level#isLessSpecificThan(Level) equal or less specific}
40 * than the specified threshold level when the queue is full.
41 *
42 * @param thresholdLevel level of events to discard
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 }