log4j2異步日誌解讀(一)AsyncAppender

 

log4j、logback、log4j2 歷史和關係,咱們就在這裏不展開講了。直接上乾貨,log4j2突出於其餘日誌的優點,異步日誌實現。html

看一個東西,首先看官網文檔 ,由於前面文章已經講解了disruptor源碼,本文主要展開說說異步日誌AsyncAppender和AsyncLogger(基於disruptor實現)。java

AsyncLogger筆者下文展開講apache

 

1、AsyncAppender

 

咱們先來看看AsyncApperder核心,就是logger將數據經過append方法放入到阻塞隊列中,隨後後臺線程從隊列中取出數據而後進行後續的操做。app

那這樣看來,就很簡單了,一個append()方法,一個後臺線程執行就是咱們要看的核心代碼了。圍繞咱們要看的類AsyncAppender,來看看類關係圖。異步

1、放入隊列

主要實現就是logger將數據經過append方法放入到阻塞隊列中。async

//AsyncAppender.java
    /**
     * Actual writing occurs here.
     *
     * @param logEvent The LogEvent.
     */
    @Override
    public void append(final LogEvent logEvent) {
        if (!isStarted()) {
            throw new IllegalStateException("AsyncAppender " + getName() + " is not active");
        }
        //建立Log4jLogEvent的對象memento
        final Log4jLogEvent memento = Log4jLogEvent.createMemento(logEvent, includeLocation);
        InternalAsyncUtil.makeMessageImmutable(logEvent.getMessage());
        //transfer(memento)將event放入隊列
        //默認ArrayBlockingQueueFactory 大小1024
        if (!transfer(memento)) {
            if (blocking) {
                if (AbstractLogger.getRecursionDepth() > 1) { // LOG4J2-1518, LOG4J2-2031
                    // If queue is full AND we are in a recursive call, call appender directly to prevent deadlock
                    AsyncQueueFullMessageUtil.logWarningToStatusLogger();
                    logMessageInCurrentThread(logEvent);
                } else {
                    // delegate to the event router (which may discard, enqueue and block, or log in current thread)
                 
                    final EventRoute route = asyncQueueFullPolicy.getRoute(thread.getId(), memento.getLevel());
                    route.logMessage(this, memento);
                }
            } else {
                error("Appender " + getName() + " is unable to write primary appenders. queue is full");
                logToErrorAppenderIfNecessary(false, memento);
            }
        }
    }

    private boolean transfer(final LogEvent memento) {
        return queue instanceof TransferQueue
            ? ((TransferQueue<LogEvent>) queue).tryTransfer(memento)
            : queue.offer(memento);
    }

如流程圖所示,首先會判斷用戶是否設置了blocking選項,默認是true,若是設置爲false,則Appender直接會ToErrorAppender,若是用戶沒有配置或者配置爲true,則會按照必定的策略來處理這些消息。策略能夠分爲2種,他們分別爲:ide

一、DefaultAsyncQueueFullPolicy---等待隊列,轉爲同步操做策略性能

public class DefaultAsyncQueueFullPolicy implements AsyncQueueFullPolicy {
    @Override
    public EventRoute getRoute(final long backgroundThreadId, final Level level) {

        // LOG4J2-471: prevent deadlock when RingBuffer is full and object
        // being logged calls Logger.log() from its toString() method
        if (Thread.currentThread().getId() == backgroundThreadId) {
            return EventRoute.SYNCHRONOUS;
        }
        return EventRoute.ENQUEUE;
    }


二、DiscardingAsyncQueueFullPolicy---按照日誌等級拋棄日誌策略this

//DiscardingAsyncQueueFullPolicy.java
    @Override
    public EventRoute getRoute(final long backgroundThreadId, final Level level) {
        if (level.isLessSpecificThan(thresholdLevel)) {
            if (discardCount.getAndIncrement() == 0) {
                LOGGER.warn("Async queue is full, discarding event with level {}. " +
                        "This message will only appear once; future events from {} " +
                        "are silently discarded until queue capacity becomes available.",
                        level, thresholdLevel);
            }
            return EventRoute.DISCARD;
        }
        return super.getRoute(backgroundThreadId, level);
    }

2、後臺線程執行後續操做。

主要就是後臺線程從隊列中取出數據而後進行後續的操做。spa

//AsyncAppender.java
private class AsyncThread extends Log4jThread {

        private volatile boolean shutdown = false;
        private final List<AppenderControl> appenders;
        private final BlockingQueue<LogEvent> queue;

        public AsyncThread(final List<AppenderControl> appenders, final BlockingQueue<LogEvent> queue) {
            super("AsyncAppender-" + THREAD_SEQUENCE.getAndIncrement());
            this.appenders = appenders;
            this.queue = queue;
            setDaemon(true);
        }

        @Override
        public void run() {
            while (!shutdown) {
                LogEvent event;
                try {
                    event = queue.take();
                    if (event == SHUTDOWN_LOG_EVENT) {
                        shutdown = true;
                        continue;
                    }
                } catch (final InterruptedException ex) {
                    break; // LOG4J2-830
                }
                event.setEndOfBatch(queue.isEmpty());
                final boolean success = callAppenders(event);
                if (!success && errorAppender != null) {
                    try {
                        errorAppender.callAppender(event);
                    } catch (final Exception ex) {
                        // Silently accept the error.
                    }
                }
            }
            // Process any remaining items in the queue.
            LOGGER.trace("AsyncAppender.AsyncThread shutting down. Processing remaining {} queue events.",
                queue.size());
            int count = 0;
            int ignored = 0;
            while (!queue.isEmpty()) {
                try {
                    final LogEvent event = queue.take();
                    if (event instanceof Log4jLogEvent) {
                        final Log4jLogEvent logEvent = (Log4jLogEvent) event;
                        logEvent.setEndOfBatch(queue.isEmpty());
                        callAppenders(logEvent);
                        count++;
                    } else {
                        ignored++;
                        LOGGER.trace("Ignoring event of class {}", event.getClass().getName());
                    }
                } catch (final InterruptedException ex) {
                    // May have been interrupted to shut down.
                    // Here we ignore interrupts and try to process all remaining events.
                }
            }
            LOGGER.trace("AsyncAppender.AsyncThread stopped. Queue has {} events remaining. "
                + "Processed {} and ignored {} events since shutdown started.", queue.size(), count, ignored);
        }

    ...
}

該線程會一直嘗試從阻塞隊列中獲取LogEvent,若是獲取成功,調用AppenderRef所引用Appender的append方法。咱們也能夠看到,AsyncAppender實際上主要是相似於中轉,日誌異步化,當消息放入阻塞隊列,返回成功,這樣可以大幅提升日誌記錄的吞吐。用戶能夠在權衡性能與日誌收集質量上進行權衡配置策略(設置blocking選項),固然也能夠設置不一樣類型的阻塞隊列已到達更好的日誌記錄吞吐。

AsyncAppender配置參數  

https://logging.apache.org/log4j/2.x/manual/appenders.html#AsyncAppender

相關文章
相關標籤/搜索