Home Ask Login Register

Developers Planet

Your answer is one click away!

Jason Stanley February 2016

Implementation of Async logging buffer

I am trying to setup async logging and want to write to log ONLY if the buffer size limit has been reached.

So everytime 8MB of log data has been written, only then flush everything to program.log

My current log4j2.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" monitorInterval="5">
            <Property name="log-path">/opt/job/log</Property>

            <RollingFile name="CONSOLE_C" fileName="${log-path}/program.log" immediateFlush="false" append="false"
                    <PatternLayout pattern="%-d{yyyy-MM-dd HH:mm:ss} [ %-5p ] [%5X{TId}] [%22X{Info}] [ %20c ] - %-m %n"/>
                            <SizeBasedTriggeringPolicy size="10240 KB" />
                    <DefaultRolloverStrategy max="15" />
            <Async name="Async" bufferSize="8000" shutdownTimeout="100" >
                    <AppenderRef ref="CONSOLE_C"/>
            <Root level="debug">
                    <AppenderRef ref="CONSOLE_C" />
            </Root >

Although I am writing most of my data to program.log but it doesnt appear to be asynchronous. The log file is being continuously written to while I am using the application.

Is there something wrong that I am doing ?


Remko Popma February 2016

You're not doing anything wrong. With asynchronous logging, immediateFlush=false in Log4j 2 effectively means "only flush when necessary or at end of batch".

Buffered logging has great performance benefits but has the drawback that often you cannot see the most recent log output because it hasn't been flushed to disk yet.

Asynchronous logging in Log4j 2 uses "smart batching": the buffer is flushed not only when it is full, but also when the queue of log events became empty and the background thread doesn't have any other work to do.

This has the benefit that your log events are immediately visible on disk, but is much more efficient than setting immediateFlush=true.

Post Status

Asked in February 2016
Viewed 2,223 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life