Home > Enterprise >  Logback - how can you tell AsyncAppender's queue is full and starts dropping events?
Logback - how can you tell AsyncAppender's queue is full and starts dropping events?

Time:12-17

Is there a way to know that logback is discarding logging events because the queue is full? Is it logged anywhere?

CodePudding user response:

Set the discardingThreshold property of the AsyncAppender to a non-zero value. This property specifies the maximum number of events that can be queued before Logback starts discarding events. When the queue is full and Logback starts discarding events, it will log a message indicating that the discarding has started.

For example, you can configure the AsyncAppender in your Logback configuration file as follows:

<appender name="ASYNC" >
<discardingThreshold>10</discardingThreshold>
<appender-ref ref="FILE" />
</appender>

In this example, Logback will log a message when the AsyncAppender's queue is full and starts discarding events, and the queue will have a maximum size of 10 events.

You can also set the discardAnyLogEventOnError property of the AsyncAppender to true to ensure that Logback discards any log event that cannot be processed due to an error. This can be useful if you want to ensure that logging does not interfere with the operation of your application.

For example:

<appender name="ASYNC" >
    <discardingThreshold>10</discardingThreshold>
    <discardAnyLogEventOnError>true</discardAnyLogEventOnError>
    <appender-ref ref="FILE" />
</appender>

In this example, Logback will discard any log event that cannot be processed due to an error, and will log a message when the AsyncAppender's queue is full and starts discarding events. The queue will have a maximum size of 10 events.

CodePudding user response:

There is no direct way to do it.

The Appender silently discards event:

AsyncAppenderBase.java

    @Override
    protected void append(E eventObject) {
        if (isQueueBelowDiscardingThreshold() && isDiscardable(eventObject)) {
            return;
        }
        preprocess(eventObject);
        put(eventObject);
    }

However, you can create your own appender by extending AsyncAppenderBase or AsyncAppender and override append method with your custom logic.

  • Related