代码改变世界

log4j2用Log4jContextSelector启动参数配置全局异步日志是如何使用disruptor

2019-06-08 11:28  chen.simon  阅读(2364)  评论(0编辑  收藏  举报

log4j2用asyncRoot配置异步日志是如何使用disruptor差异有几个:

给disruptor实例的EventFactory不同

此处EventFactory采用的是RingBufferLogEvent.FACTORY,newInstance逻辑大致是:

public RingBufferLogEvent newInstance() {
    final RingBufferLogEvent result = new RingBufferLogEvent();
    if (Constants.ENABLE_THREADLOCALS) {
        result.messageText = new StringBuilder(Constants.INITIAL_REUSABLE_MESSAGE_SIZE);
        result.parameters = new Object[10];
    }
    return result;
}

创建的是 RingBufferLogEvent实例。该对象封装了日志输出的执行动作等。

给disruptor的threadFactory的参数不同

thread factory的名字是"AsyncLogger[" + contextName + "]"

给disruptor的EventHandler不同

此处用RingBufferLogEventHandler,对接RingBufferLogEvent实例处理日志写出。

给disrupto的Translator不同

此处用RingBufferLogEventTranslator,对接RingBufferLogEvent实例组装日志信息。

事件进入队列方式不同

封装了一个叫tryPublish的方法进入队列。