You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2015/12/23 14:38:44 UTC
[05/11] logging-log4j2 git commit: LOG4J2-1080
DiscardingAsyncEventRouter drops events below a certain log level when the
queue is getting too full
LOG4J2-1080 DiscardingAsyncEventRouter drops events below a certain log level when the queue is getting too full
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d48b2b7e
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d48b2b7e
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d48b2b7e
Branch: refs/heads/master
Commit: d48b2b7ed2200a38019dd46507c25395410b3791
Parents: 992ed1e
Author: rpopma <rp...@apache.org>
Authored: Wed Dec 23 22:10:21 2015 +0900
Committer: rpopma <rp...@apache.org>
Committed: Wed Dec 23 22:10:21 2015 +0900
----------------------------------------------------------------------
.../core/async/DiscardingAsyncEventRouter.java | 93 ++++++++++++++++++++
1 file changed, 93 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d48b2b7e/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DiscardingAsyncEventRouter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DiscardingAsyncEventRouter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DiscardingAsyncEventRouter.java
new file mode 100644
index 0000000..2f5805a
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DiscardingAsyncEventRouter.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.async;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.status.StatusLogger;
+
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Discarding router extends the DefaultAsyncEventRouter by first verifying if the queue is fuller than the specified
+ * threshold ratio; if this is the case, log events {@linkplain Level#isMoreSpecificThan(Level) more specific} than
+ * the specified threshold level are dropped. If this is not the case, the {@linkplain DefaultAsyncEventRouter
+ * default routing rules hold.
+ */
+public class DiscardingAsyncEventRouter extends DefaultAsyncEventRouter {
+ private static final Logger LOGGER = StatusLogger.getLogger();
+
+ private final int thresholdQueueRemainingCapacity;
+ private final Level thresholdLevel;
+ private final AtomicLong discardCount = new AtomicLong();
+
+ /**
+ * Constructs a router that will discard events {@linkplain Level#isLessSpecificThan(Level) equal or less specific}
+ * than the specified threshold level when the queue is fuller than the specified threshold ratio.
+ *
+ * @param queueSize size of the queue
+ * @param thresholdQueueFilledRatio threshold ratio: if queue is fuller than this, start discarding events
+ * @param thresholdLevel level of events to discard
+ */
+ public DiscardingAsyncEventRouter(final int queueSize, final float thresholdQueueFilledRatio,
+ final Level thresholdLevel) {
+ thresholdQueueRemainingCapacity = calcThresholdQueueRemainingCapacity(queueSize, thresholdQueueFilledRatio);
+ this.thresholdLevel = Objects.requireNonNull(thresholdLevel, "thresholdLevel");
+ }
+
+ private static int calcThresholdQueueRemainingCapacity(final int queueSize,
+ final float thresholdQueueFilledRatio) {
+ if (thresholdQueueFilledRatio >= 1F) {
+ return 0;
+ }
+ if (thresholdQueueFilledRatio <= 0F) {
+ return queueSize;
+ }
+ return (int) ((1 - thresholdQueueFilledRatio) * queueSize);
+ }
+
+ @Override
+ public EventRoute getRoute(final long backgroundThreadId, final Level level, final int queueSize,
+ final int queueRemainingCapacity) {
+ if (queueRemainingCapacity <= thresholdQueueRemainingCapacity && level.isLessSpecificThan(thresholdLevel)) {
+ if (discardCount.getAndIncrement() == 0) {
+ LOGGER.warn("Async remaining queue capacity is {}, discarding event with level {}. " +
+ "This message will only appear once; future events from {} " +
+ "are silently discarded until queue capacity becomes available.",
+ queueRemainingCapacity, level, thresholdLevel);
+ }
+ return EventRoute.DISCARD;
+ }
+ return super.getRoute(backgroundThreadId, level, queueSize, queueRemainingCapacity);
+ }
+
+ public static long getDiscardCount(final AsyncEventRouter router) {
+ if (router instanceof DiscardingAsyncEventRouter) {
+ return ((DiscardingAsyncEventRouter) router).discardCount.get();
+ }
+ return 0;
+ }
+
+ public int getThresholdQueueRemainingCapacity() {
+ return thresholdQueueRemainingCapacity;
+ }
+
+ public Level getThresholdLevel() {
+ return thresholdLevel;
+ }
+}