Spring-Boot
Kafka
Logging
Warning Elimination
Best Practices

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

  1. Include dependencies: Add the Kafka and Logback dependencies to your pom.xml or build.gradle.
xml
1   <!-- Add this in your pom.xml for Maven projects -->
2   <dependency>
3       <groupId>org.apache.kafka</groupId>
4       <artifactId>kafka-clients</artifactId>
5       <version>your.kafka.version</version>
6   </dependency>
7   <dependency>
8       <groupId>com.github.danielwegener</groupId>
9       <artifactId>logback-kafka-appender</artifactId>
10       <version>0.2.0</version>
11   </dependency>
  1. Configure Logback: In src/main/resources/, create or edit logback-spring.xml to include a Kafka appender:
xml
1   <configuration>
2       <appender name="KAFKA" class="com.github.danielwegener.logback.kafka.KafkaAppender">
3           <topic>your-log-topic</topic>
4           <keyingStrategy class="com.github.danielwegener.logback.kafka.keying.NoKeyKeyingStrategy"/>
5           <producerConfig>bootstrap.servers=your.kafka.broker:9092</producerConfig>
6           <encoder>
7               <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{0}: %msg%n</pattern>
8           </encoder>
9       </appender>
10       <root level="info">
11           <appender-ref ref="KAFKA" />
12       </root>
13   </configuration>
  1. Set producer properties: Put Kafka producer settings inside the producerConfig tags. 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.servers property.

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.
ConcernBest Practice Advice
PerformanceUse asynchronous logging
Failure HandlingConfigure fallback mechanisms
SecuritySecure Kafka connections; mask sensitive logs
EfficiencyEnable compression on Kafka messages

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design