Spring Boot logging to Kafka: eliminating warnings and best practices
Codemia Knowledge Hub ·
To send Spring Boot logs to Kafka, configure Logback with a Kafka appender that publishes log events to a topic. Warnings in this setup usually come from conflicting Kafka or Logback versions on the classpath, a misconfigured encoder, or wrong bootstrap.servers settings. Kafka is useful here because it collects logs centrally, where monitoring, analysis, and alerting systems can consume them.
How Spring Boot logging is set up
Spring Boot uses Commons Logging for its internal logging but leaves the underlying implementation open. It ships default configurations for Java Util Logging, Log4J2, and Logback, and Logback is the default in most Spring Boot applications.
Adding a Kafka appender to Logback
- Include dependencies: Add the Kafka and Logback dependencies to your
pom.xmlorbuild.gradle.
- Configure Logback: In
src/main/resources/, create or editlogback-spring.xmlto include a Kafka appender:
- Set producer properties: Put Kafka producer settings inside the
producerConfigtags. Any property the Kafka producer supports can go there.
Eliminating common warnings
- Classpath conflicts: make sure there are no conflicting versions of Kafka or Logback libraries on the classpath. Incompatible versions produce warnings.
- Serializer warnings: if log events are not serialized correctly, you may get warnings. Configure the encoder appropriately.
- Broker connectivity: warnings about failing to connect to brokers usually mean network issues or an incorrect
bootstrap.serversproperty.
Best practices for logging to Kafka
- Log asynchronously: wrap the Kafka appender in an asynchronous appender to limit the impact on application performance.
- Plan for Kafka being down: configure the appender to drop messages once a buffer is full, or to fall back to another appender (such as a file) so logs are not lost.
- Secure the connection: if the cluster uses SSL or SASL, add the matching properties to
producerConfig. - Mask sensitive data: mask sensitive values or do not log them at all.
- Enable compression: compress Kafka messages to reduce network traffic.
| Concern | Best Practice Advice |
| Performance | Use asynchronous logging |
| Failure Handling | Configure fallback mechanisms |
| Security | Secure Kafka connections; mask sensitive logs |
| Efficiency | Enable compression on Kafka messages |
Related reading
- spring-integration-kafka config consumer to receive message from specify partition
- spring-kafka application.properties configuration for JAAS/SASL not working
- Spring-Kafka Concurrency Property
- Spring-Kafka How to pass the kafka topic from the application.yml
- Spring-Boot logging with log4j2?
- Spring Actuator + Kafka Streams - Add kafka stream status to health check endpoint
- Spring-Boot MVC Template Not Loading 404 Not Found
- Spring-boot required a bean named 'entityManagerFactory' that could not be found
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.