服务器之家

服务器之家 > 正文

log4j2日志异步打印(实例讲解)

时间:2021-01-23 12:13     来源/作者:wunan23的日志

log4j2支持日志异步打印,日志异步输出的好处在于,使用单独的进程来执行日志打印的功能,可以提高日志执行效率,减少日志功能对正常业务的影响。

异步日志在程序的classpath需要加载disruptor-3.0.0.jar或者更高的版本。

Asynchronous Loggers是一个新增特性在Log4j 2 ,可以实现完全异步也可以和同步混合使用,还可以只异步化Appender,以提升系统性能,官方数据显示混合没有完全异步化效果好。

1,完全异步模式:

这种异步日志方式,不需要修改原来的配置文件,Logger仍然使用<root> and <logger>

只需要在主程序代码开头,加一句系统属性的代码:

?
1
System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");

或者设置启动参数:

?
1
DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector

2,异步和非异步混合输出模式

在配置文件中Logger使用<asyncRoot> 或 <asyncLogger>,而且<asyncRoot> 或 <asyncLogger>可以和<root> 或 <logger>混合使用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
 
<!-- No need to set system property "Log4jContextSelector" to any value
when using <asyncLogger> or <asyncRoot>. -->
 
<Configuration status="WARN">
<Appenders>
<!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
<RandomAccessFile name="RandomAccessFile" fileName="asyncWithLocation.log"
immediateFlush="false" append="false">
<PatternLayout>
<Pattern>%d %p %class{1.} [%t] %location %m %ex%n</Pattern>
</PatternLayout>
</RandomAccessFile>
</Appenders>
<Loggers>
<!-- pattern layout actually uses location, so we need to include it -->
<AsyncLogger name="com.foo.Bar" level="trace" includeLocation="true">
<AppenderRef ref="RandomAccessFile"/>
</AsyncLogger>
<Root level="info" includeLocation="true">
<AppenderRef ref="RandomAccessFile"/>
</Root>
</Loggers>
</Configuration>

这里需要注意的是,如果使用<asyncRoot> 或 <asyncLogger>,includeLocation="true"是必须要设置才会有类路径等一些信息打印出来。

3,只异步化Appender

在<Appenders>标签里增加如下<Async>标签

?
1
2
3
<Async name="asyncAppender" includeLocation="true">
<AppenderRef ref="RandomAccessFile"/>
</Async>

然后在<Root>或者<Logger>标签中引用asyncAppender即可,这里includeLocation是增加在<Async>标签中的。

?
1
2
3
<Root level="info">
<AppenderRef ref="RandomAccessFile"/>
</Root>

无论是完全异步模式还是混合模式,在Appender标签中,immediateFlush属性无论为true或者false,效果都是和设置false是一样的。

4,性能对比

完全异步 > 混合模式 > 只异步化Appender > 同步

5,疑问

使用混合异步模式进行多线程写日志测试的时候,偶尔会出现日志没有写完的情况。是不是主线程执行完了,不会等待写日志的线程执行完,就把进程给停掉了? 在主线程的最后sleep几秒,就没有再出现日志写不完的情况了。

以上这篇log4j2日志异步打印(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://blog.163.com/wunan_23/blog/static/195562320201741045941799/

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部

773
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40