001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.async;
018
019import org.apache.logging.log4j.Level;
020import org.apache.logging.log4j.Logger;
021import org.apache.logging.log4j.status.StatusLogger;
022
023import java.util.Objects;
024import java.util.concurrent.atomic.AtomicLong;
025
026/**
027 * Discarding router extends the DefaultAsyncQueueFullPolicy by first verifying if the queue is fuller than the specified
028 * threshold ratio; if this is the case, log events {@linkplain Level#isMoreSpecificThan(Level) more specific} than
029 * the specified threshold level are dropped. If this is not the case, the {@linkplain DefaultAsyncQueueFullPolicy
030 * default routing rules hold.
031 */
032public class DiscardingAsyncQueueFullPolicy extends DefaultAsyncQueueFullPolicy {
033    private static final Logger LOGGER = StatusLogger.getLogger();
034
035    private final Level thresholdLevel;
036    private final AtomicLong discardCount = new AtomicLong();
037
038    /**
039     * Constructs a router that will discard events {@linkplain Level#isLessSpecificThan(Level) equal or less specific}
040     * than the specified threshold level when the queue is full.
041     *
042     * @param thresholdLevel level of events to discard
043     */
044    public DiscardingAsyncQueueFullPolicy(final Level thresholdLevel) {
045        this.thresholdLevel = Objects.requireNonNull(thresholdLevel, "thresholdLevel");
046    }
047
048    @Override
049    public EventRoute getRoute(final long backgroundThreadId, final Level level) {
050        if (level.isLessSpecificThan(thresholdLevel)) {
051            if (discardCount.getAndIncrement() == 0) {
052                LOGGER.warn("Async queue is full, discarding event with level {}. " +
053                        "This message will only appear once; future events from {} " +
054                        "are silently discarded until queue capacity becomes available.",
055                        level, thresholdLevel);
056            }
057            return EventRoute.DISCARD;
058        }
059        return super.getRoute(backgroundThreadId, level);
060    }
061
062    public static long getDiscardCount(final AsyncQueueFullPolicy router) {
063        if (router instanceof DiscardingAsyncQueueFullPolicy) {
064            return ((DiscardingAsyncQueueFullPolicy) router).discardCount.get();
065        }
066        return 0;
067    }
068
069    public Level getThresholdLevel() {
070        return thresholdLevel;
071    }
072}