---
title: "开启log4j2异步日志"
author: "wutao8818"
date: "2014-11-14T16:53:05Z"
tags: []
categories: ["编程语言"]
slug: "log4j2"
draft: false
lastmod: "2014-11-14T17:30:57Z"
id: 2156196
url: "https://wutaoo.iteye.com/blog/2156196"
---

![](http://dl2.iteye.com/upload/attachment/0103/1892/90135d99-d925-3706-a136-2cb94f79eba3.png)

**看到这样的性能提升，可能每个程序员都会对她着迷片刻。**

官方文档
<http://logging.apache.org/log4j/2.x/manual/async.html>

1、使用方式一：全局开启


> Making All Loggers Asynchronous


需要disruptor-3.0.0.jar


> This is simplest to configure and gives the best performance. To make all loggers asynchronous, add the disruptor jar to the classpath and set the system property Log4jContextSelector to org.apache.logging.log4j.core.async.AsyncLoggerContextSelector.


<span style="color:red">开启全局异步日志的最简单的方法是设置Log4jContextSelector系统属性为
org.apache.logging.log4j.core.async.AsyncLoggerContextSelector</span>


> By default, location is not passed to the I/O thread by asynchronous loggers. If one of your layouts or custom filters needs location information, you need to set "includeLocation=true" in the configuration of all relevant loggers, including the root logger.


<span style="color:red">默认location是不会被发送到异步日志。如果需要location信息，需要设置includeLocation=true
</span>


```xml
<Loggers>
    <Root level="info" includeLocation="false">
      <AppenderRef ref="RandomAccessFile"/>
    </Root>
  </Loggers>
```



2、使用方式二：混合同步异步日志


> Requires disruptor-3.0.0.jar or higher on the classpath. There is no need to set system property "Log4jContextSelector" to any value.


需要 disruptor-3.0.0.jar，但不需要在系统属性中配置Log4jContextSelector值。


```xml
<AsyncLogger name="com.foo.Bar" level="trace" includeLocation="true">
      <AppenderRef ref="RandomAccessFile"/>
    </AsyncLogger>
```