参考

Making All Loggers Asynchronous

将原本的log4j2同步日志转化为“完全异步日志模式”。官方文档:

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 log4j2.contextSelector to org.apache.logging.log4j.core.async.AsyncLoggerContextSelector.

Log4j-2.9 and higher require disruptor-3.3.4.jar or higher on the classpath. Prior to Log4j-2.9, disruptor-3.0.0.jar or higher was required.

document ...

第一步,添加相应的disruptor库

各个依赖包的版本需要对应。如下:

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
<!-- Asynchronous Loggers internally use the Disruptor, a lock-free
inter-thread communication library, instead of queues, resulting in
higher throughput and lower latency.-->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>

pom.xml

第二步,设置系统属性log4j2.contextSelector

一种简便的方式是在相应classpath下创建一个叫log4j2.component.properties的文件(maven项目直接放在main/resources就好),内容如下:

Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector

log4j2.component.properties

PS. Log4jContextSelector的第一个字母必须大写

第三步,检验!

首先你需要允许log4j2输出自身的debug信息,log4j2.xml:

<Configuration status="DEBUG">

然后启动你的web项目,在ide或者catalina.out里观察debug信息,类似下面这样就说明成功:

log4j2笔记 #02# 启用异步日志的更多相关文章

  1. log4j2用asyncRoot配置异步日志是如何使用disruptor

    用asyncRoot配置对应的对接disruptor类是AsyncLoggerConfigDisruptor,用Log4jContextSelector启动参数配置全局异步的对应的对接disrupto ...

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

    与 log4j2用asyncRoot配置异步日志是如何使用disruptor差异有几个: 给disruptor实例的EventFactory不同 此处EventFactory采用的是RingBuffe ...

  3. Log4j2中的同步日志与异步日志

    1.背景 Log4j 2中记录日志的方式有同步日志和异步日志两种方式,其中异步日志又可分为使用AsyncAppender和使用AsyncLogger两种方式. 2.Log4j2中的同步日志 所谓同步日 ...

  4. log4j2异步日志配置及官方文档的问题澄清

    配置及demo 方法一全部打开 加启动参数 -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextS ...

  5. 一次鞭辟入里的 Log4j2 异步日志输出阻塞问题的定位

    一次鞭辟入里的 Log4j2 日志输出阻塞问题的定位 问题现象 线上某个应用的某个实例突然出现某些次请求服务响应极慢的情况,有几次请求超过 60s 才返回,并且通过日志发现,服务线程并没有做什么很重的 ...

  6. log4j2异步日志解读(二)AsyncLogger

    前文已经讲了log4j2的AsyncAppender的实现[log4j2异步日志解读(一)AsyncAppender],今天我们看看AsyncLogger的实现. 看了这个图,应该很清楚AsyncLo ...

  7. log4j2异步日志解读(一)AsyncAppender

    log4j.logback.log4j2 历史和关系,我们就在这里不展开讲了.直接上干货,log4j2突出于其他日志的优势,异步日志实现. 看一个东西,首先看官网文档 ,因为前面文章已经讲解了disr ...

  8. spring boot:配置druid数据库连接池(开启sql防火墙/使用log4j2做异步日志/spring boot 2.3.2)

    一,druid数据库连接池的功能? 1,Druid是阿里巴巴开发的号称为监控而生的数据库连接池 它的优点包括: 可以监控数据库访问性能 SQL执行日志 SQL防火墙 2,druid的官方站: http ...

  9. spring boot:使用log4j2做异步日志打印(spring boot 2.3.1)

    一,为什么要使用log4j2?     log4j2是log4j的升级版,     升级后更有优势:     性能更强/吞吐量大/支持异步     功能扩展/支持插件/支持自定义级别等     这些优 ...

随机推荐

  1. golang 的 buffered channel 及 unbuffered channel

    The channel is divided into two categories: unbuffered and buffered. (1) Unbuffered channelFor unbuf ...

  2. 异常:分为 严重性错误:Error 异常:Exception

    异常:是在运行时期发生的不正常情况.在java中用类的形式对不正常情况进行了描述和封装对象描述不正常的情况的类,就称为异常类以前:正常流程代码和问题处理代码相结合现在将正常流程代码和问题处理代码分离, ...

  3. 8款不错的 CI/CD工具

    Jenkins Jenkins是CI市场中最知名且最常见的名号之一.其最初是由Sun公司的一位工程师打造的一个辅助项目,并迅速扩展为最大的开源CI工具之一,可帮助工程团队实现自动化部署.顺带一提:我们 ...

  4. node-express根据请求,判断PC和移动端

    function getMachine(req) { var deviceAgent = req.headers["user-agent"].toLowerCase(); var ...

  5. js模拟队列----小优先队列

    队列:先进先出,后进后出 var Queue = (function(){ var item = new WeakMap(); class Queue{ constructor(){ item.set ...

  6. node微信公众号开发---域名绑定

    var TOKEN='weixin'; //必须与测试号所填写的Token相同 function checkSignature(params,token){ var key=[token,params ...

  7. 安装Cuda9.0+cudnn7.3.1+tensorflow-gpu1.13.1

    我的安装版本: win10 x64 VS2015 conda python 3.7 显卡 GTX 940mx Cuda 9.0 cudnn v7.3.1 Tensorflow-gpu 1.13.1 1 ...

  8. rosetta mpi编译时出现 MPI has not been declared 错误

    安装openmpi2.1.0版本,编译mpi rosetta时出现 MPI has not been declared 错误,经过一系列的摸索发现安装openmpi-1.6.5就可以顺利编译,降低版本 ...

  9. (已解决)Eclipsez中打不开c++文件,显示Editor could not be initialized.

    新建的游戏导入Eclipse能正常运行,配置什么的都弄好了,游戏运行无任何问题!问题是:关闭Eclipse后,重新打开,就会出现An internal error occurred during: & ...

  10. python爬虫-基础入门-爬取整个网站《2》

    python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...