'How do you flush a buffered log4j FileAppender?
In log4j, when using a FileAppender with BufferedIO=true and BufferSize=xxx properties (i.e. buffering is enabled), I want to be able to flush the log during normal shutdown procedure. Any ideas on how to do this?
Solution 1:[1]
When shutting down the LogManager:
LogManager.shutdown();
all buffered logs get flushed.
Solution 2:[2]
public static void flushAll() {
final LoggerContext logCtx = ((LoggerContext) LogManager.getContext());
for(final org.apache.logging.log4j.core.Logger logger : logCtx.getLoggers()) {
for(final Appender appender : logger.getAppenders().values()) {
if(appender instanceof AbstractOutputStreamAppender) {
((AbstractOutputStreamAppender) appender).getManager().flush();
}
}
}
}
Solution 3:[3]
Maybe you could override WriterAppender#shouldFlush( LoggingEvent ), so it would return true for a special logging category, like log4j.flush.now, and then you call:
LoggerFactory.getLogger("log4j.flush.now").info("Flush")
Solution 4:[4]
Sharing my experience with using "Andrey Kurilov"'s code example, or at least simmilar.
What I actually wanted to achieve was to implement an asynchronous log entries with manual flush (immediateFlush = false) to ensure an idle buffers content will be flushed before the bufferSize is reached.
The initial performance results were actually comparable with the ones achieved with the AsyncAppender - so I think it is a good alternative of it.
The AsyncAppender is using a separate thread (and additional dependency to disruptor jar), which makes it more performant, but with the cost of more CPU and even more disk flushing(no matter with high load flushes are made on batches).
So if you want to save disk IO operations and CPU load, but still want to ensure your buffers will be flushed asynchronously at some point, that is the way to go.
Solution 5:[5]
Try:
LogFactory.releaseAll();
Solution 6:[6]
I have written an appender that fixes this, see GitHub or use name.wramner.log4j:FlushAppender in Maven. It can be configured to flush on events with high severity and it can make the appenders unbuffered when it receives a specific message, for example "Shutting down". Check the unit tests for configuration examples. It is free, of course.
Solution 7:[7]
The only solution that worked for me is waiting for a while:
private void flushAppender(Appender appender) {
// this flush seems to be useless
((AbstractOutputStreamAppender<?>) appender).getManager().flush();
try {
Thread.sleep(500); // wait for log4j to flush logs
} catch (InterruptedException ignore) {
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kalle Richter |
| Solution 2 | Andrey Kurilov |
| Solution 3 | Ondra Žižka |
| Solution 4 | Alex Ciocan |
| Solution 5 | rsp |
| Solution 6 | ewramner |
| Solution 7 | Ferran Maylinch |
