Reputation: 1338
I have Async appender with bufferSize="512"
.
In some situations I need to forcibly flush the buffer. How can I do that?
I don't what to close the LogManager, so LogManager.shutdown();
is not an option.
Please help!
Update: Here is the part of my log4j2.xml:
<Kafka name="Kafka" topic="logs-gexd-default">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %C{1}:%L - %m%n</pattern>
</PatternLayout>
<Property name="bootstrap.servers">${kafkaAddress}</Property>
</Kafka>
<Async name="AsyncKafka" bufferSize="512" blocking="false">
<AppenderRef ref="Kafka"/>
</Async>
I have a Kafka appender inside of Async appender. Unfortunately neither Kafka appender not Async appender has immediateFlush field.
Upvotes: 1
Views: 4243
Reputation: 36754
The buffer used by AsyncAppender does not have the concept of flushing. The events in this buffer have not been processed yet so they cannot be flushed. The speed at which events are taken off the queue depends on the underlying Appenders.
Are you looking for a way to discard all events currently in the queue? If so, can you elaborate on your use case? (The application will simply add more LogEvents to the queue so I can't see how discarding queued events would be useful...)
Update after comments:
I would recommend that you reconfigure instead of stopping some Appenders while keeping others running. If you have monitorInterval
set, you can change the configuration file to remove the Appenders you want to stop. As part of the reconfiguration process, Log4j2 will ensure that log events that were queued in the Async Appender are fully processed before the Appenders are stopped.
Upvotes: 1
Reputation: 1546
This answer might resolve your problem... You just need to find a particular appender instead of flushing all of them. Though, the flush message might not be desirable in your case...
You can also use this technique to write a method that will write a given message then flush immediately and then call setImmediateFlush(false) for future messages...
Upvotes: 0