简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM