Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

java - Need Spring kafka custom logging

I have a standard spring kafka setup as follows,

@Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> feedbackStreamListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }
@KafkaListener(topics = ("my-topic"),groupId = ("groupId"),containerFactory = "listenerContainerFactory")
    public void myListener(@Payload String message) {
        System.out.println("Received Message : " + message);
        // do some heavy processing
    }

Now I need to have custom logging (generate logs from my app) on 3 scenarios,

  1. When I boot the consumer lets say the kafka cluster is down or I provided the wrong bootstrap server, I need to custom log it.
  2. When the consumer starts successfully, just before polling provide a custom log.
  3. When there is a custom message converter, and if there is a deserialisation issue, custom log it.
question from:https://stackoverflow.com/questions/65916521/need-spring-kafka-custom-logging

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See KafkaEvent implementations: https://docs.spring.io/spring-kafka/docs/current/reference/html/#events.

So, when you catch ConsumerFailedToStartEvent in the @EventListener, you can log whatever you want and so on with the ConsumerStartedEvent.

There is no even for deserialization issues, but you definitely can use a GenericErrorHandler to log something when error from the consumer happens: https://docs.spring.io/spring-kafka/docs/current/reference/html/#annotation-error-handling


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...