Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use log4j's FileAppenders asynchronously?

Tags:

java

log4j

I work on a low-latency trading application. We'd like to increase the amount of lof4j logging that we write to file, whilst minimising the impact on our end-to-end processing time.

What is the recommended way of doing this? I think FileAppender.append is synchronous, so we need to do something a bit smarter than that....

like image 606
Phil Harvey Avatar asked Feb 10 '11 12:02

Phil Harvey


People also ask

Is log4j Async by default?

Mule runtime engine (Mule) 4 uses Log4j 2 asynchronous logging by default, so the logging operation happens in a separate thread from the message processing, and the latter can continue without having to wait for the log operation to complete.

What is asynchronous logging?

In synchronous logging, the data is directly written to the destination i.e a file or a database. In Asynchronous logging, the data is written to a queue first and then to the destination.

Is slf4j synchronous?

Yes, it's synchronous by default.


3 Answers

bear in mind that AsyncAppender adds a thread per appender & that increasing the amount of logging you do may mean a substantial increase in the amount of string concatentation going on which often means a substantial amount of string processing/munging/formatting which can be pretty expensive (relative to the latency involved in a low latency trading app anyway).

like image 38
Matt Avatar answered Sep 18 '22 17:09

Matt


If you need to log from a multithreaded application slf4j and its implementation logback are much better choice.

like image 38
Boris Pavlović Avatar answered Sep 17 '22 17:09

Boris Pavlović


Yes, the appenders are synchronous. You want something like this:

http://www.spartanjava.com/2009/asynchronous-logging-with-log4j/

like image 123
Danny Thomas Avatar answered Sep 20 '22 17:09

Danny Thomas