Log4J Async will slow you down

Everyone loves Log4J. This article is an asynchronous setting The author doesn't know much about the logging system. I've been messing around on the WEB and it didn't work, so I'll summarize the article.

Conclusion

Somehow it's faster than Async in my environment.

Motivation

Since the bottlenecks of WEB applications are logging and DB, I wanted a fast one.

official

http://logging.apache.org/log4j/2.x/manual/async.html It is 6-68 times faster. It's amazing. You shouldn't use it for audit trail logs or Exception logs! It's said, but it's okay. use.

request

According to the IPA documentation (project members read and gave me specs),

How to write

Write AsyncLogger surrounded by Async

The former didn't work as I tried it. why? → Use AsyncLogger

RollingRandomAccessFileAppender, FileAppender and RollingFileAppender

RandomAccessFile seems to be fast, so adopted

log4j2.xml I don't really know. difficult.

の前にlog4j.properties

log4j.properties It happens if you do not create and put it, so put it roughly. I think it's a request from FW Vert.x, but I'm not chasing it

log4j.rootLogger=ERROR, console
log4j.logger.xxx=ERROR, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d [%-5p-%c] %m%n

log4j.xml

I'll write it honestly

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <RollingRandomAccessFile name="AppLog" fileName="logs/app.log" filePattern="logs/old/app-%d{yyyy-MM-dd}.log.gz"
                 ignoreExceptions="false">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <TimeBasedTriggeringPolicy/>
    </RollingRandomAccessFile>
    <RollingRandomAccessFile name="AccessLog" fileName="logs/access.log" filePattern="logs/old/access-%d{yyyy-MM-dd}.log.gz"
                 ignoreExceptions="false">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <TimeBasedTriggeringPolicy/>
    </RollingRandomAccessFile>
    <RollingRandomAccessFile name="ErrorLog" fileName="logs/error.log" filePattern="logs/old/error-%d{yyyy-MM-dd}.log.gz"
                 ignoreExceptions="false">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <TimeBasedTriggeringPolicy/>
    </RollingRandomAccessFile>
    <RollingRandomAccessFile name="SQLLog" fileName="logs/sql.log" filePattern="logs/old/sql-%d{yyyy-MM-dd}.log.gz"
                 ignoreExceptions="false">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <TimeBasedTriggeringPolicy/>
    </RollingRandomAccessFile>
    <Async name="AppAsync">
      <AppenderRef ref="Console"/>
    </Async>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="AppAsync"/>
    </Root>
    <AsyncLogger level="debug" name="access-log">
      <AppenderRef ref="AccessLog"/>
    </AsyncLogger>
    <AsyncLogger level="debug" name="error-log">
      <AppenderRef ref="ErrorLog"/>
    </AsyncLogger>
    <AsyncLogger level="debug" name="sql-log">
      <AppenderRef ref="SQLLog"/>
    </AsyncLogger>
  </Loggers>
</Configuration>

performance

Roughly hit with the ʻab` command Check on Celeron's super slow file server machine

asynchronous

Requests per second:    347.96 [#/sec](mean)

Sync

Requests per second:    2830.04 [#/sec](mean)

Synchronization is faster. It's a lie. I feel something is wrong.

Measurement important

The situation where synchronization is faster. Well I do not know. You have to measure it properly. that's all.

Recommended Posts

Log4J Async will slow you down