Log4j1、Logback 以及 Log4j2 性能测试对
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">点击上方“</span><span style="color: black;">芋道源码</span><span style="color: black;">”,<span style="color: black;">选取</span>“设为星标”</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">做积极的人,而不是积极废人!</span></p><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">源码精品专栏</p>
</span> <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">精尽 Dubbo 原理与源码 69 篇</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">精尽 Netty 原理与源码 61 篇</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">中文<span style="color: black;">仔细</span>注释的开源项目</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">Java 并发源码合集</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">RocketMQ 源码合集</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">Sharding-JDBC 源码解析合集</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">Spring MVC 和 Secur</span></a>ity 源码合集</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">MyCAT 源码解析合集</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">源自</span>:https://dwz.cn/woS9vbcQ</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">环境</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">准备</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">测试</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">环境</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">jdk:1.7.0_79</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cpu:i5-4570@3.20GHz 4核</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">eclipse:3.7</p>操作系统:win7
<h1 style="color: black; text-align: left; margin-bottom: 10px;">准备</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1.log4j:1.7.21</strong></p><span style="color: black;"><<span style="color: black;">dependency</span>></span> <span style="color: black;"><<span style="color: black;">groupId</span>></span>org.slf4j<span style="color: black;"></<span style="color: black;">groupId</span>></span> <span style="color: black;"><<span style="color: black;">artifactId</span>></span>slf4j-log4j12<span style="color: black;"></<span style="color: black;">artifactId</span>></span> <span style="color: black;"><<span style="color: black;">version</span>></span>1.7.21<span style="color: black;"></<span style="color: black;">version</span>></span><span style="color: black;"></<span style="color: black;">dependency</span>></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">log4j.xml</strong></p><span style="color: black;"><?xml version="1.0" encoding="UTF-8"?></span><span style="color: black;"><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"></span><span style="color: black;"><<span style="color: black;">log4j:configuration</span> <span style="color: black;">xmlns:log4j</span>=<span style="color: black;">http://jakarta.apache.org/log4j/</span>></span> <span style="color: black;"><<span style="color: black;">appender</span> <span style="color: black;">name</span>=<span style="color: black;">"myConsole"</span> <span style="color: black;">class</span>=<span style="color: black;">"org.apache.log4j.ConsoleAppender"</span>></span> <span style="color: black;"><<span style="color: black;">layout</span> <span style="color: black;">class</span>=<span style="color: black;">"org.apache.log4j.PatternLayout"</span>></span> <span style="color: black;"><<span style="color: black;">param</span> <span style="color: black;">name</span>=<span style="color: black;">"ConversionPattern"</span> <span style="color: black;">value</span>=<span style="color: black;">"[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n"</span>/></span> <span style="color: black;"></<span style="color: black;">layout</span>></span> <span style="color: black;"><!--过滤器设置输出的级别 --></span> <span style="color: black;"><<span style="color: black;">filter</span> <span style="color: black;">class</span>=<span style="color: black;">"org.apache.log4j.varia.LevelRangeFilter"</span>></span> <span style="color: black;"><<span style="color: black;">param</span> <span style="color: black;">name</span>=<span style="color: black;">"levelMin"</span> <span style="color: black;">value</span>=<span style="color: black;">"debug"</span> /></span> <span style="color: black;"><<span style="color: black;">param</span> <span style="color: black;">name</span>=<span style="color: black;">"levelMax"</span> <span style="color: black;">value</span>=<span style="color: black;">"warn"</span> /></span> <span style="color: black;"><<span style="color: black;">param</span> <span style="color: black;">name</span>=<span style="color: black;">"AcceptOnMatch"</span> <span style="color: black;">value</span>=<span style="color: black;">"true"</span> /></span> <span style="color: black;"></<span style="color: black;">filter</span>></span> <span style="color: black;"></<span style="color: black;">appender</span>></span> <span style="color: black;"><<span style="color: black;">appender</span> <span style="color: black;">name</span>=<span style="color: black;">"myFile"</span> <span style="color: black;">class</span>=<span style="color: black;">"org.apache.log4j.DailyRollingFileAppender"</span>></span> <span style="color: black;"><<span style="color: black;">param</span> <span style="color: black;">name</span>=<span style="color: black;">"File"</span> <span style="color: black;">value</span>=<span style="color: black;">"log4jTest.log"</span> /></span> <span style="color: black;"><<span style="color: black;">param</span> <span style="color: black;">name</span>=<span style="color: black;">"Append"</span> <span style="color: black;">value</span>=<span style="color: black;">"true"</span> /></span> <span style="color: black;"><<span style="color: black;">param</span> <span style="color: black;">name</span>=<span style="color: black;">"DatePattern"</span> <span style="color: black;">value</span>=<span style="color: black;">".yyyy-MM-dd.log"</span> /></span> <span style="color: black;"><<span style="color: black;">layout</span> <span style="color: black;">class</span>=<span style="color: black;">"org.apache.log4j.PatternLayout"</span>></span> <span style="color: black;"><<span style="color: black;">param</span> <span style="color: black;">name</span>=<span style="color: black;">"ConversionPattern"</span> <span style="color: black;">value</span>=<span style="color: black;">"[%t] - %m%n"</span> /></span> <span style="color: black;"></<span style="color: black;">layout</span>></span> <span style="color: black;"></<span style="color: black;">appender</span>></span> <span style="color: black;"><<span style="color: black;">appender</span> <span style="color: black;">name</span>=<span style="color: black;">"async_file"</span> <span style="color: black;">class</span>=<span style="color: black;">"org.apache.log4j.AsyncAppender"</span>></span> <span style="color: black;"><<span style="color: black;">param</span> <span style="color: black;">name</span>=<span style="color: black;">"BufferSize"</span> <span style="color: black;">value</span>=<span style="color: black;">"32"</span> /></span> <span style="color: black;"><<span style="color: black;">appender-ref</span> <span style="color: black;">ref</span>=<span style="color: black;">"myFile"</span> /></span> <span style="color: black;"></<span style="color: black;">appender</span>></span> <span style="color: black;"><<span style="color: black;">logger</span> <span style="color: black;">name</span>=<span style="color: black;">"org.logTest"</span> <span style="color: black;">additivity</span>=<span style="color: black;">"false"</span>></span> <span style="color: black;"><<span style="color: black;">level</span> <span style="color: black;">value</span>=<span style="color: black;">"info"</span> /></span> <span style="color: black;"><<span style="color: black;">appender-ref</span> <span style="color: black;">ref</span>=<span style="color: black;">"async_file"</span> /></span> <span style="color: black;"><!-- 同步:FILE 异步:async_file --></span> <span style="color: black;"></<span style="color: black;">logger</span>></span><span style="color: black;"></<span style="color: black;">log4j:configuration</span>></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2.logback:1.1.7</strong></p><span style="color: black;"><<span style="color: black;">dependency</span>></span> <span style="color: black;"><<span style="color: black;">groupId</span>></span>ch.qos.logback<span style="color: black;"></<span style="color: black;">groupId</span>></span> <span style="color: black;"><<span style="color: black;">artifactId</span>></span>logback-classic<span style="color: black;"></<span style="color: black;">artifactId</span>></span> <span style="color: black;"><<span style="color: black;">version</span>></span>1.1.7<span style="color: black;"></<span style="color: black;">version</span>></span><span style="color: black;"></<span style="color: black;">dependency</span>></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">logback.xml</strong></p><span style="color: black;"><<span style="color: black;">configuration</span>></span> <span style="color: black;"><<span style="color: black;">appender</span> <span style="color: black;">name</span>=<span style="color: black;">"STDOUT"</span> <span style="color: black;">class</span>=<span style="color: black;">"ch.qos.logback.core.ConsoleAppender"</span>></span> <span style="color: black;"><!-- encoder 默认配置为PatternLayoutEncoder --></span> <span style="color: black;"><<span style="color: black;">encoder</span>></span> <span style="color: black;"><<span style="color: black;">pattern</span>></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</p> <span style="color: black;"></<span style="color: black;">pattern</span>></span> <span style="color: black;"></<span style="color: black;">encoder</span>></span> <span style="color: black;"></<span style="color: black;">appender</span>></span> <span style="color: black;"><<span style="color: black;">appender</span> <span style="color: black;">name</span>=<span style="color: black;">"FILE"</span> <span style="color: black;">class</span>=<span style="color: black;">"ch.qos.logback.core.FileAppender"</span>></span> <span style="color: black;"><<span style="color: black;">file</span>></span>testFile.log<span style="color: black;"></<span style="color: black;">file</span>></span> <span style="color: black;"><<span style="color: black;">append</span>></span>true<span style="color: black;"></<span style="color: black;">append</span>></span> <span style="color: black;"><<span style="color: black;">encoder</span>></span> <span style="color: black;"><<span style="color: black;">pattern</span>></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">[%t] - %m%n</p> <span style="color: black;"></<span style="color: black;">pattern</span>></span> <span style="color: black;"></<span style="color: black;">encoder</span>></span> <span style="color: black;"></<span style="color: black;">appender</span>></span> <span style="color: black;"><!-- 异步输出 --></span> <span style="color: black;"><<span style="color: black;">appender</span> <span style="color: black;">name</span>=<span style="color: black;">"ASYNC"</span> <span style="color: black;">class</span>=<span style="color: black;">"ch.qos.logback.classic.AsyncAppender"</span>></span> <span style="color: black;"><<span style="color: black;">discardingThreshold</span>></span>0<span style="color: black;"></<span style="color: black;">discardingThreshold</span>></span> <span style="color: black;"><<span style="color: black;">appender-ref</span> <span style="color: black;">ref</span>=<span style="color: black;">"FILE"</span> /></span> <span style="color: black;"></<span style="color: black;">appender</span>></span> <span style="color: black;"><<span style="color: black;">logger</span> <span style="color: black;">name</span>=<span style="color: black;">"org.logTest"</span> <span style="color: black;">level</span>=<span style="color: black;">"INFO"</span> <span style="color: black;">additivity</span>=<span style="color: black;">"false"</span>></span> <span style="color: black;"><<span style="color: black;">appender-ref</span> <span style="color: black;">ref</span>=<span style="color: black;">"ASYNC"</span> /></span> <span style="color: black;"><!-- 同步:FILE 异步:ASYNC--></span> <span style="color: black;"></<span style="color: black;">logger</span>></span> <span style="color: black;"><<span style="color: black;">root</span> <span style="color: black;">level</span>=<span style="color: black;">"ERROR"</span>></span> <span style="color: black;"><<span style="color: black;">appender-ref</span> <span style="color: black;">ref</span>=<span style="color: black;">"STDOUT"</span> /></span> <span style="color: black;"></<span style="color: black;">root</span>></span><span style="color: black;"></<span style="color: black;">configuration</span>></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.log4j2:2.6.2</strong></p><span style="color: black;"><<span style="color: black;">dependency</span>></span> <span style="color: black;"><<span style="color: black;">groupId</span>></span>org.apache.logging.log4j<span style="color: black;"></<span style="color: black;">groupId</span>></span> <span style="color: black;"><<span style="color: black;">artifactId</span>></span>log4j-core<span style="color: black;"></<span style="color: black;">artifactId</span>></span> <span style="color: black;"><<span style="color: black;">version</span>></span>2.6.2<span style="color: black;"></<span style="color: black;">version</span>></span><span style="color: black;"></<span style="color: black;">dependency</span>></span><span style="color: black;"><<span style="color: black;">dependency</span>></span> <span style="color: black;"><<span style="color: black;">groupId</span>></span>org.apache.logging.log4j<span style="color: black;"></<span style="color: black;">groupId</span>></span> <span style="color: black;"><<span style="color: black;">artifactId</span>></span>log4j-slf4j-impl<span style="color: black;"></<span style="color: black;">artifactId</span>></span> <span style="color: black;"><<span style="color: black;">version</span>></span>2.6.2<span style="color: black;"></<span style="color: black;">version</span>></span><span style="color: black;"></<span style="color: black;">dependency</span>></span><span style="color: black;"><<span style="color: black;">dependency</span>></span> <span style="color: black;"><<span style="color: black;">groupId</span>></span>com.lmax<span style="color: black;"></<span style="color: black;">groupId</span>></span> <span style="color: black;"><<span style="color: black;">artifactId</span>></span>disruptor<span style="color: black;"></<span style="color: black;">artifactId</span>></span> <span style="color: black;"><<span style="color: black;">version</span>></span>3.3.4<span style="color: black;"></<span style="color: black;">version</span>></span><span style="color: black;"></<span style="color: black;">dependency</span>></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">log4j2.xml</strong></p><span style="color: black;"><?xml version="1.0" encoding="UTF-8"?></span><span style="color: black;"><!--设置log4j2的<span style="color: black;">自己</span>log级别为warn --></span><span style="color: black;"><<span style="color: black;">configuration</span> <span style="color: black;">status</span>=<span style="color: black;">"warn"</span>></span> <span style="color: black;"><<span style="color: black;">appenders</span>></span> <span style="color: black;"><<span style="color: black;">console</span> <span style="color: black;">name</span>=<span style="color: black;">"Console"</span> <span style="color: black;">target</span>=<span style="color: black;">"SYSTEM_OUT"</span>></span> <span style="color: black;"><<span style="color: black;">PatternLayout</span> <span style="color: black;">pattern</span>=<span style="color: black;">"[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"</span> /></span> <span style="color: black;"></<span style="color: black;">console</span>></span> <span style="color: black;"><<span style="color: black;">RollingFile</span> <span style="color: black;">name</span>=<span style="color: black;">"RollingFileInfo"</span> <span style="color: black;">fileName</span>=<span style="color: black;">"info.log"</span> <span style="color: black;">filePattern</span>=<span style="color: black;">"${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log"</span>></span> <span style="color: black;"><<span style="color: black;">Filters</span>></span> <span style="color: black;"><<span style="color: black;">ThresholdFilter</span> <span style="color: black;">level</span>=<span style="color: black;">"INFO"</span> /></span> <span style="color: black;"><<span style="color: black;">ThresholdFilter</span> <span style="color: black;">level</span>=<span style="color: black;">"WARN"</span> <span style="color: black;">onMatch</span>=<span style="color: black;">"DENY"</span> <span style="color: black;">onMismatch</span>=<span style="color: black;">"NEUTRAL"</span> /></span> <span style="color: black;"></<span style="color: black;">Filters</span>></span> <span style="color: black;"><<span style="color: black;">PatternLayout</span> <span style="color: black;">pattern</span>=<span style="color: black;">"[%t] - %m%n"</span> /></span> <span style="color: black;"><<span style="color: black;">Policies</span>></span> <span style="color: black;"><<span style="color: black;">TimeBasedTriggeringPolicy</span> /></span> <span style="color: black;"><<span style="color: black;">SizeBasedTriggeringPolicy</span> <span style="color: black;">size</span>=<span style="color: black;">"100 MB"</span> /></span> <span style="color: black;"></<span style="color: black;">Policies</span>></span> <span style="color: black;"></<span style="color: black;">RollingFile</span>></span> <span style="color: black;"><<span style="color: black;">RandomAccessFile</span> <span style="color: black;">name</span>=<span style="color: black;">"RandomAccessFile"</span> <span style="color: black;">fileName</span>=<span style="color: black;">"asyncWithLocation.log"</span> <span style="color: black;">immediateFlush</span>=<span style="color: black;">"false"</span> <span style="color: black;">append</span>=<span style="color: black;">"true"</span>></span> <span style="color: black;"><<span style="color: black;">PatternLayout</span>></span> <span style="color: black;"><<span style="color: black;">Pattern</span>></span>[%t] - %m%n<span style="color: black;"></<span style="color: black;">Pattern</span>></span> <span style="color: black;"></<span style="color: black;">PatternLayout</span>></span> <span style="color: black;"></<span style="color: black;">RandomAccessFile</span>></span> <span style="color: black;"></<span style="color: black;">appenders</span>></span> <span style="color: black;"><<span style="color: black;">loggers</span>></span> <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> includeLocation="true"></p> </AsyncLogger> -->
</span> <span style="color: black;"><<span style="color: black;">Root</span> <span style="color: black;">level</span>=<span style="color: black;">"info"</span> <span style="color: black;">includeLocation</span>=<span style="color: black;">"true"</span>></span> <span style="color: black;"><<span style="color: black;">AppenderRef</span> <span style="color: black;">ref</span>=<span style="color: black;">"RollingFileInfo"</span> /></span> <span style="color: black;"></<span style="color: black;">Root</span>></span> <span style="color: black;"></<span style="color: black;">loggers</span>></span><span style="color: black;"></<span style="color: black;">configuration</span>></span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">测试</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">准备50条线程<span style="color: black;">同期</span>记录1000000条数据,<span style="color: black;">而后</span>统计时间,<span style="color: black;">仔细</span>代码如下:</p><span style="color: black;">import</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">java.util.concurrent.CountDownLatch;</p><span style="color: black;">import</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> org.slf4j.Logger;</p><span style="color: black;">import</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> org.slf4j.LoggerFactory;</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">App</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">private</span> <span style="color: black;">static</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Logger log = LoggerFactory.getLogger(App.class);</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span><span style="color: black;">(String[] args)</span> <span style="color: black;">throws</span> InterruptedException </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">int</span> messageSize = <span style="color: black;">1000000</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">int</span> threadSize = <span style="color: black;">50</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">final</span> <span style="color: black;">int</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> everySize = messageSize / threadSize;</p> <span style="color: black;">final</span>CountDownLatch cdl =<span style="color: black;">new</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> CountDownLatch(threadSize);</p> <span style="color: black;">long</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> startTime = System.currentTimeMillis();</p> <span style="color: black;">for</span> (<span style="color: black;">int</span> ts = <span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">; ts < threadSize; ts++) {</p> <span style="color: black;">new</span> Thread(<span style="color: black;">new</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Runnable() {</p> <span style="color: black;">@Override</span> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">run</span><span style="color: black;">()</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">for</span> (<span style="color: black;">int</span> es = <span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">; es < everySize; es++) {</p> log.info(<span style="color: black;">"======info"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> cdl.countDown();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }).start();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> cdl.await();</p> <span style="color: black;">long</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">endTime = System.currentTimeMillis();</p> System.out.println(<span style="color: black;">"log4j1:messageSize = "</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> + messageSize</p> + <span style="color: black;">",threadSize = "</span> + threadSize + <span style="color: black;">",costTime = "</span> + (endTime - startTime) + <span style="color: black;">"ms"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">log4j1和logback的同步和异步分别修改为对应的appender就行了</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">log4j2的异步方式<span style="color: black;">供给</span>了2中模式:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.全局开启</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设置Log4jContextSelector系统属性为:</p>org.apache.logging.log4j.core.async.AsyncLoggerContextSelectorSystem.setProperty(<span style="color: black;">"Log4jContextSelector"</span>, <span style="color: black;">"org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.混合同步异步模式</p>不需要设置Log4jContextSelector,<span style="color: black;">然则</span>需要<span style="color: black;">运用</span>AsyncLogger标签
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">更加多</span><span style="color: black;">仔细</span>参考官方文档:http://logging.apache.org/log4j/2.x/manual/async.html#AllAsync</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">结果</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">分别测试完以后统计成表格如下:</p><img src="https://mmbiz.qpic.cn/mmbiz_png/JdLkEI9sZfe39A5picdNfpqh5RI8Hlk8TqNnNicqEKLC77LsIZ33BibepJ8p1ynRbWR9jicMic78mM8N5QOsD4EXQ4A/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">img<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">log4j2的异步模式表现了绝对的性能<span style="color: black;">优良</span>,<span style="color: black;">优良</span><span style="color: black;">重点</span>得益于Disruptor框架的<span style="color: black;">运用</span></p>LMAX Disruptor technology. Asynchronous Loggers internally<span style="color: black;">use</span> the Disruptor, a <span style="color: black;">lock</span>-free inter-<span style="color: black;">thread</span> communication <span style="color: black;">library</span>, instead <span style="color: black;">of</span> queues, resulting <span style="color: black;">in</span>higher throughput<span style="color: black;">and</span> <span style="color: black;">lower</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> latency.</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一个无锁的线程间通信库代替了原来的队列</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">更加多</span>Disruptor :</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http://developer.51cto.com/art/201306/399370.htm</p>http://ifeve.com/disruptor/
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">欢迎加入我的知识星球,<span style="color: black;">一块</span>探讨架构,交流源码。加入方式,<strong style="color: blue;"><span style="color: black;">长按下方二维码噢</span></strong>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/JdLkEI9sZffVTY1cbQPIhsMGJcNWFwIDn1HomDs3KMQdVdSpmRkicibAFj9FzVtNtQxAtyAhyqmo1N8aibviaU4Qxg/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">已在知识星球更新源码解析如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/JdLkEI9sZffUCBXGfVvL7KsDg2TYdzcIrqcNU2F1G46mzD0dTd7pBNV8NLyWIOXAwm2iadbdybHVL65dhTmg8YQ/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/JdLkEI9sZffUCBXGfVvL7KsDg2TYdzcIxYiajjv1MycMd7pkQbQ5WPBcEsHNKYnoFRYqbjjloz00f8ibDic1dv2yQ/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/JdLkEI9sZffUCBXGfVvL7KsDg2TYdzcIicGZMIzqr2IIQHsAZdkG0tAibAOTQRHVfOZvicA9Qo2ica4HgX72ibE6jMQ/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/JdLkEI9sZffVTY1cbQPIhsMGJcNWFwIDS4C592uCNnJsdPCVw72HHvM9ictRn1vUOW7C7Wa8ccNXtK1O86OibGYg/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>
回顾历史,我们不难发现:无数先辈用鲜血和生命铺就了中华民族复兴的康庄大道。 期待与你深入交流,共探知识的无穷魅力。
页:
[1]