What happens to logging events when the AsyncAppender buffer fills in non-blocking mode?
Environment
- JBoss Enterprise Application Platform
- log4j
Issue
- We are using the AsyncAppender and have its blocking mode disabled:
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Threshold" value="INFO"/>
<param name="Blocking" value="false"/>
<appender-ref ref="FILE"/>
</appender>
- What will then happen if a thread tries to log something while the AsyncAppender's buffer is full?
Resolution
- Logging events in this case will likely be skipped over, discarded, and never logged.
- To be specific, when the async logging buffer is full, threads do not wait for it to empty when logging and so don't place their log event in the buffer. They instead place them in a map of discarded log events.
- This discarded log map only keeps a max of one log event from every category/class, and it will keep the first occurrence of the highest level log event.
- So lets say I have a class using a logger matching its package/class name, and that it logs the following level events in this order when the buffer is full and blocking mode is disabled:
TRACE discardedmessage1
INFO discardedmessage2
ERROR loggedmessage3
TRACE discardedmessage4
INFO discardedmessage5
ERROR discardedmessage6
- We will then only keep that first ERROR event in the discard map. And when the Dispatcher thread empties the buffer again, it will grab all the preserved events from discard map and log a message like the following for our example above and any other logger:
ERROR [my.package.ClassName] Discarded 6 messages due to full event buffer including: loggedmessage3
SBR
Category
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.