Search code examples
loggingresilience4j

Prevent verbose logging on Resilience4j Retry's onException()


I'm getting an excessive amount of logs in log files when logging error messages inside the onException() function. When the circuit breaker enters in OPEN state, I'm getting a log line for every blocked call. Is there a native way to prevent this in R4j ? There is no interest in logging the same information indefinitely if the call is the same.

Here is a code snapshot:

onMessage method

 @Override
public void onMessage(final Message message) {
    //Decorate message operation
    CheckedRunnable myCheckedRunnable = Decorators
            .ofCheckedRunnable(() -> processMessage(message))
            .withCircuitBreaker(myCircuitBreaker)
            .withRetry(myRetry)
            .decorate();

    //Trigger message operation and catch exceptions
    Try.run(myCheckedRunnable ).onSuccess(aVoid -> acknowledgeMessage(message)).recover(throwable -> onException(throwable, message));
}

onException method

protected Void onException(final Throwable t, final Message message) {
        try {

            String messageId = message != null? message.getJMSMessageID(): null;

            if (Arrays.asList(Resilience4jConfig.adapterSourceListenerSkipExceptions).stream().noneMatch(c -> c.isInstance(t))) {

                logger.error(
                        "My Error message printed when blocked call is caught: {} - {}",messageId  , t.toString());


                if(session != null){
                    session.recover();
                }
            }else{
                logger.warn(
                        "My other warning message printed when blocked call is caught: {} - {}", messageId, t.toString());

                message.acknowledge();
            }
        } catch (JMSException e) {
            logger.error("An error- {}", e);
        }
        return null;
    }

Thank you in advance for you help!


Solution

  • The most simplest way is to add CallNotPermittedException to the list of ignored exception so that they are not retried. Everything else is your code. You can decide when and how to log a catched exception.

    RetryConfig retryConfig = RetryConfig.custom()
        .ignoreExceptions(CallNotPermittedException.class)
        .build();