Kafka Log Analysis
Guide to analyzing Apache Kafka logs for troubleshooting and diagnostics.
Log File Locations
Section titled “Log File Locations”Broker Logs
Section titled “Broker Logs”| Log File | Purpose | Key Information |
|---|---|---|
server.log | Main broker log | Errors, warnings, startup/shutdown |
controller.log | Controller operations | Leader elections, partition assignments |
state-change.log | Partition state changes | ISR changes, leadership changes |
kafka-authorizer.log | Authorization decisions | ACL evaluations |
kafka-request.log | Request logging | Client requests (if enabled) |
log-cleaner.log | Log compaction | Compaction progress, errors |
kafkaServer-gc.log | JVM GC logs | GC events, pause times |
Default Locations
Section titled “Default Locations”# Linux/Standard installation/var/log/kafka//opt/kafka/logs/
# Kubernetes/var/log/containers/kafka-*.log
# Dockerdocker logs <kafka-container>Log Patterns
Section titled “Log Patterns”Critical Patterns (Immediate Action)
Section titled “Critical Patterns (Immediate Action)”| Pattern | Meaning | Action |
|---|---|---|
FATAL | Fatal error | Investigate immediately |
OutOfMemoryError | Heap exhausted | Increase heap, check for leaks |
KafkaStorageException | Disk failure | Check disk health |
OfflinePartitionsCount > 0 | Partitions offline | Restore brokers |
# Find critical errorsgrep -E "FATAL|OutOfMemoryError|KafkaStorageException" server.logError Patterns (Investigation Required)
Section titled “Error Patterns (Investigation Required)”| Pattern | Meaning | Action |
|---|---|---|
ERROR | Error condition | Investigate root cause |
NotLeaderForPartition | Stale metadata | Usually transient |
UnknownTopicOrPartition | Topic doesn’t exist | Create topic or fix config |
Connection.*refused | Network issue | Check connectivity |
Authentication failed | Auth error | Check credentials |
# Find errorsgrep "ERROR" server.log | tail -100
# Filter by componentgrep "ERROR.*\[Controller\]" controller.logWarning Patterns (Monitor)
Section titled “Warning Patterns (Monitor)”| Pattern | Meaning | Action |
|---|---|---|
WARN | Warning condition | Monitor frequency |
ISR shrunk | Replica fell behind | Check replica health |
Connection.*timed out | Slow network | Investigate latency |
Request.*too large | Large request | Check client config |
# Count warnings by typegrep "WARN" server.log | cut -d: -f4 | sort | uniq -c | sort -rnState Change Patterns
Section titled “State Change Patterns”| Pattern | Meaning |
|---|---|
Partition.*Leader | Leadership change |
ISR.*expanded | Replica rejoined ISR |
ISR.*shrunk | Replica left ISR |
state.*OnlinePartition | Partition came online |
state.*OfflinePartition | Partition went offline |
# Track leadership changesgrep "Leader" state-change.log | tail -50
# Track ISR changesgrep "ISR" state-change.log | tail -50Log Analysis Commands
Section titled “Log Analysis Commands”Basic Search
Section titled “Basic Search”# Search for patterngrep "pattern" server.log
# Case-insensitive searchgrep -i "error" server.log
# Search with contextgrep -B 5 -A 5 "Exception" server.log
# Search multiple filesgrep "ERROR" /var/log/kafka/*.logTime-Based Analysis
Section titled “Time-Based Analysis”# Filter by time rangeawk '/2024-01-15 10:/ && /2024-01-15 11:/' server.log
# Last hourgrep "$(date -d '1 hour ago' '+%Y-%m-%d %H')" server.log
# Count errors per hourgrep "ERROR" server.log | cut -d' ' -f1-2 | cut -d: -f1-2 | uniq -cPattern Frequency
Section titled “Pattern Frequency”# Most common errorsgrep "ERROR" server.log | \ sed 's/.*ERROR/ERROR/' | \ cut -d: -f1-2 | \ sort | uniq -c | sort -rn | head -20
# Most common exceptionsgrep -oE "[A-Z][a-zA-Z]+Exception" server.log | \ sort | uniq -c | sort -rnCorrelation Analysis
Section titled “Correlation Analysis”# Find events around a timestampgrep "2024-01-15 10:30" server.log | head -50
# Correlate across filestimestamp="2024-01-15 10:30"for log in server.log controller.log state-change.log; do echo "=== $log ===" grep "$timestamp" $log | head -10doneDebug Logging
Section titled “Debug Logging”Enable Debug Dynamically
Section titled “Enable Debug Dynamically”# Enable debug for specific loggerkafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type broker-loggers \ --entity-name 0 \ --alter \ --add-config kafka.server=DEBUG
# Verify logger levelkafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type broker-loggers \ --entity-name 0 \ --describe
# Reset to defaultkafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type broker-loggers \ --entity-name 0 \ --alter \ --delete-config kafka.serverCommon Debug Loggers
Section titled “Common Debug Loggers”| Logger | Purpose |
|---|---|
kafka.server | Server operations |
kafka.controller | Controller operations |
kafka.network | Network/request handling |
kafka.log | Log management |
kafka.request.logger | Request details |
kafka.authorizer.logger | ACL decisions |
kafka.coordinator.group | Consumer group coordination |
kafka.coordinator.transaction | Transaction coordination |
Enable via Configuration
Section titled “Enable via Configuration”# Debug controllerlog4j.logger.kafka.controller=DEBUG
# Debug networklog4j.logger.kafka.network=DEBUG
# Debug replicationlog4j.logger.kafka.server.ReplicaManager=DEBUGlog4j.logger.kafka.server.ReplicaFetcherThread=DEBUG
# Debug authorizationlog4j.logger.kafka.authorizer.logger=DEBUG
# Request logging (verbose)log4j.logger.kafka.request.logger=DEBUGRequest Logging
Section titled “Request Logging”Enable Request Logging
Section titled “Enable Request Logging”log4j.logger.kafka.request.logger=DEBUG
# Separate file for requestslog4j.appender.requestAppender=org.apache.log4j.RollingFileAppenderlog4j.appender.requestAppender.File=${kafka.logs.dir}/kafka-request.loglog4j.appender.requestAppender.MaxFileSize=100MBlog4j.appender.requestAppender.MaxBackupIndex=10log4j.logger.kafka.request.logger=DEBUG,requestAppenderlog4j.additivity.kafka.request.logger=falseRequest Log Format
Section titled “Request Log Format”[timestamp] Completed request:[RequestType] with correlation id [id]in queue time ms:[queue_time],local time ms:[local_time],remote time ms:[remote_time],throttle time ms:[throttle_time],response size:[size]Analyze Request Latency
Section titled “Analyze Request Latency”# Extract latency metricsgrep "Completed request" kafka-request.log | \ awk '{ for(i=1;i<=NF;i++) { if($i ~ /local/) print $i, $(i+1), $(i+2), $(i+3) } }' | \ sort -t: -k4 -rn | head -20Common Log Scenarios
Section titled “Common Log Scenarios”Leader Election
Section titled “Leader Election”[2024-01-15 10:30:00,123] INFO [Controller id=1] Partition [topic,0]has been elected as leader at epoch 5 (kafka.controller.KafkaController)
[2024-01-15 10:30:00,125] INFO [Partition topic-0 broker=1]ISR updated to [1,2,3] (kafka.cluster.Partition)Interpretation: Leadership change occurred. Check for broker failures if unexpected.
ISR Shrink
Section titled “ISR Shrink”[2024-01-15 10:30:00,123] WARN [Partition topic-0 broker=1]Shrinking ISR from [1,2,3] to [1,2] (kafka.cluster.Partition)Interpretation: Broker 3 fell behind and was removed from ISR. Check:
- Broker 3 health
- Network connectivity
- Disk I/O on broker 3
Consumer Group Rebalance
Section titled “Consumer Group Rebalance”[2024-01-15 10:30:00,123] INFO [GroupCoordinator 0]:Member consumer-1 in group my-group has left (kafka.coordinator.group.GroupCoordinator)
[2024-01-15 10:30:01,456] INFO [GroupCoordinator 0]:Preparing to rebalance group my-group (kafka.coordinator.group.GroupCoordinator)Interpretation: Consumer left group, triggering rebalance. Check:
- Consumer health
- Session timeout settings
- Processing time
Authentication Failure
Section titled “Authentication Failure”[2024-01-15 10:30:00,123] INFO [SocketServer listenerType=BROKER, nodeId=1]Failed authentication with /10.0.0.100(Authentication failed during authentication due to:Authentication failed: credentials do not match) (kafka.network.SocketServer)Interpretation: Client failed to authenticate. Check:
- Client credentials
- SASL configuration
- User exists in SCRAM store
Disk Error
Section titled “Disk Error”[2024-01-15 10:30:00,123] ERROR [Log partition=topic-0 dir=/var/kafka-logs]Error while flushing log (kafka.log.Log)java.io.IOException: No space left on deviceInterpretation: Disk full. Action:
- Add storage
- Reduce retention
- Delete old topics
Log Rotation Configuration
Section titled “Log Rotation Configuration”log4j.properties
Section titled “log4j.properties”# Configure rolling file appenderlog4j.appender.kafkaAppender=org.apache.log4j.RollingFileAppenderlog4j.appender.kafkaAppender.File=${kafka.logs.dir}/server.loglog4j.appender.kafkaAppender.MaxFileSize=100MBlog4j.appender.kafkaAppender.MaxBackupIndex=10log4j.appender.kafkaAppender.layout=org.apache.log4j.PatternLayoutlog4j.appender.kafkaAppender.layout.ConversionPattern=[%d] %p %m (%c)%n
# Root loggerlog4j.rootLogger=INFO, kafkaAppenderSystem logrotate
Section titled “System logrotate”/var/log/kafka/*.log { daily rotate 7 compress delaycompress missingok notifempty copytruncate}Log Aggregation
Section titled “Log Aggregation”Structured Logging
Section titled “Structured Logging”# JSON format for log aggregationlog4j.appender.kafkaAppender.layout=net.logstash.log4j.JSONEventLayoutV1Forwarding to Centralized System
Section titled “Forwarding to Centralized System”# Filebeat configurationfilebeat.inputs: - type: log enabled: true paths: - /var/log/kafka/*.log multiline: pattern: '^\[' negate: true match: after fields: service: kafka environment: production
output.elasticsearch: hosts: ["elasticsearch:9200"] index: "kafka-logs-%{+yyyy.MM.dd}"Related Documentation
Section titled “Related Documentation”- Troubleshooting Overview - Troubleshooting guide
- Common Errors - Error reference
- Diagnosis - Diagnostic procedures
- Monitoring - Metrics and alerting