Skip to content

Commit 95fc922

Browse files
tomazfernandesgaryrussell
authored andcommitted
Log KafkaBackoffException at DEBUG Level
Resolves #2009 The expected KafkaBackoffException's message is being logged at WARN level. Make KafkaBackoffException be logged in DEBUG level Remove coupling between KafkaMessageListenerContainer and KafkaBackoffException Document KafkaBackOffException Log Level Change Doc polishing.
1 parent 65a7fd2 commit 95fc922

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

spring-kafka-docs/src/main/asciidoc/retrytopic.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,25 @@ public RetryTopicConfiguration myOtherRetryTopic(KafkaTemplate<Integer, MyPojo>
702702
}
703703
----
704704
====
705+
706+
[[change-kboe-logging-level]]
707+
==== Changing KafkaBackOffException Logging Level
708+
709+
When a message in the retry topic is not due for consumption, a `KafkaBackOffException` is thrown. Such exceptions are logged by default at `DEBUG` level, but you can change this behavior by setting an error handler customizer in the `ListenerContainerFactoryConfigurer` in a `@Configuration` class.
710+
711+
For example, to change the logging level to WARN you might add:
712+
713+
====
714+
[source, java]
715+
----
716+
@Bean(name = RetryTopicInternalBeanNames.LISTENER_CONTAINER_FACTORY_CONFIGURER_NAME)
717+
public ListenerContainerFactoryConfigurer listenerContainer(KafkaConsumerBackoffManager kafkaConsumerBackoffManager,
718+
DeadLetterPublishingRecovererFactory deadLetterPublishingRecovererFactory,
719+
@Qualifier(RetryTopicInternalBeanNames
720+
.INTERNAL_BACKOFF_CLOCK_BEAN_NAME) Clock clock) {
721+
ListenerContainerFactoryConfigurer configurer = new ListenerContainerFactoryConfigurer(kafkaConsumerBackoffManager, deadLetterPublishingRecovererFactory, clock);
722+
configurer.setErrorHandlerCustomizer(commonErrorHandler -> ((DefaultErrorHandler) commonErrorHandler).setLogLevel(KafkaException.Level.WARN));
723+
return configurer;
724+
}
725+
----
726+
====

spring-kafka-docs/src/main/asciidoc/whats-new.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ See <<delegating-serialization>> for more information.
7878
The property `stripPreviousExceptionHeaders` is now `true` by default.
7979

8080
See <<dlpr-headers>> for more information.
81+
82+
[[x28-kafka-back-off-exception-log-level]]
83+
==== KafkaBackOffException Log Level Changes
84+
85+
The KafkaBackOffException thrown when using the retryable topics feature is now logged at DEBUG level.
86+
87+
See <<change-kboe-logging-level>> if you need to change the logging level back to WARN or set it to any other level.

spring-kafka/src/main/java/org/springframework/kafka/listener/KafkaMessageListenerContainer.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -2495,12 +2495,7 @@ private RuntimeException doInvokeRecordListener(final ConsumerRecord<K, V> recor
24952495
commitOffsetsIfNeeded(record);
24962496
}
24972497
catch (KafkaException ke) {
2498-
if (ke.contains(KafkaBackoffException.class)) {
2499-
this.logger.warn(ke.getMessage());
2500-
}
2501-
else {
2502-
ke.selfLog(ERROR_HANDLER_THREW_AN_EXCEPTION, this.logger);
2503-
}
2498+
ke.selfLog(ERROR_HANDLER_THREW_AN_EXCEPTION, this.logger);
25042499
return ke;
25052500
}
25062501
catch (RuntimeException ee) {

spring-kafka/src/main/java/org/springframework/kafka/retrytopic/ListenerContainerFactoryConfigurer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2021 the original author or authors.
2+
* Copyright 2018-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.beans.factory.annotation.Qualifier;
3030
import org.springframework.core.log.LogAccessor;
31+
import org.springframework.kafka.KafkaException;
3132
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
3233
import org.springframework.kafka.listener.AcknowledgingConsumerAwareMessageListener;
3334
import org.springframework.kafka.listener.CommonErrorHandler;
@@ -144,6 +145,7 @@ private CommonErrorHandler createErrorHandler(DeadLetterPublishingRecoverer dead
144145
DefaultErrorHandler errorHandler = new DefaultErrorHandler(deadLetterPublishingRecoverer,
145146
new FixedBackOff(0, 0));
146147
errorHandler.setCommitRecovered(true);
148+
errorHandler.setLogLevel(KafkaException.Level.DEBUG);
147149
this.errorHandlerCustomizer.accept(errorHandler);
148150
return errorHandler;
149151
}

0 commit comments

Comments
 (0)