Chapter 12. Configuring logging for Kafka components
Configure the logging levels of Kafka components directly in the configuration properties. You can also change the broker levels dynamically for Kafka brokers, Kafka Connect, and MirrorMaker 2.
Increasing the log level detail, such as from INFO to DEBUG, can aid in troubleshooting a Kafka cluster. However, more verbose logs may also negatively impact performance and make it more difficult to diagnose issues.
12.1. Configuring Kafka logging properties Copy linkLink copied to clipboard!
Kafka components use the Log4j framework for error logging. By default, logging configuration is read from the classpath or config directory using the following properties files:
-
log4j.propertiesfor Kafka and ZooKeeper -
connect-log4j.propertiesfor Kafka Connect and MirrorMaker 2
If they are not set explicitly, loggers inherit the log4j.rootLogger logging level configuration in each file. You can change the logging level in these files. You can also add and set logging levels for other loggers.
You can change the location and name of logging properties file using the KAFKA_LOG4J_OPTS environment variable, which is used by the start script for the component.
Passing the name and location of the logging properties file used by Kafka brokers
su - kafka export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:/my/path/to/log4j.properties"; \ /opt/kafka/bin/kafka-server-start.sh \ /opt/kafka/config/server.properties
su - kafka
export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:/my/path/to/log4j.properties"; \
/opt/kafka/bin/kafka-server-start.sh \
/opt/kafka/config/server.properties
Passing the name and location of the logging properties file used by ZooKeeper
su - kafka export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:/my/path/to/log4j.properties"; \ /opt/kafka/bin/zookeeper-server-start.sh -daemon \ /opt/kafka/config/zookeeper.properties
su - kafka
export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:/my/path/to/log4j.properties"; \
/opt/kafka/bin/zookeeper-server-start.sh -daemon \
/opt/kafka/config/zookeeper.properties
Passing the name and location of the logging properties file used by Kafka Connect
su - kafka export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:/my/path/to/connect-log4j.properties"; \ /opt/kafka/bin/connect-distributed.sh \ /opt/kafka/config/connect-distributed.properties
su - kafka
export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:/my/path/to/connect-log4j.properties"; \
/opt/kafka/bin/connect-distributed.sh \
/opt/kafka/config/connect-distributed.properties
Passing the name and location of the logging properties file used by MirrorMaker 2
su - kafka export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:/my/path/to/connect-log4j.properties"; \ /opt/kafka/bin/connect-mirror-maker.sh \ /opt/kafka/config/connect-mirror-maker.properties
su - kafka
export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:/my/path/to/connect-log4j.properties"; \
/opt/kafka/bin/connect-mirror-maker.sh \
/opt/kafka/config/connect-mirror-maker.properties
12.2. Dynamically change logging levels for Kafka broker loggers Copy linkLink copied to clipboard!
Kafka broker logging is provided by broker loggers in each broker. Dynamically change the logging level for broker loggers at runtime without having to restart the broker.
You can also reset broker loggers dynamically to their default logging levels.
Prerequisites
- Streams for Apache Kafka is installed on each host, and the configuration files are available.
- Kafka is running.
Procedure
Switch to the
kafkauser:su - kafka
su - kafkaCopy to Clipboard Copied! Toggle word wrap Toggle overflow List all the broker loggers for a broker by using the
kafka-configs.shtool:/opt/kafka/bin/kafka-configs.sh --bootstrap-server <broker_address> --describe --entity-type broker-loggers --entity-name BROKER-ID
/opt/kafka/bin/kafka-configs.sh --bootstrap-server <broker_address> --describe --entity-type broker-loggers --entity-name BROKER-IDCopy to Clipboard Copied! Toggle word wrap Toggle overflow For example, for broker
0:/opt/kafka/bin/kafka-configs.sh --bootstrap-server localhost:9092 --describe --entity-type broker-loggers --entity-name 0
/opt/kafka/bin/kafka-configs.sh --bootstrap-server localhost:9092 --describe --entity-type broker-loggers --entity-name 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow This returns the logging level for each logger:
TRACE,DEBUG,INFO,WARN,ERROR, orFATAL.For example:
#... kafka.controller.ControllerChannelManager=INFO sensitive=false synonyms={} kafka.log.TimeIndex=INFO sensitive=false synonyms={}#... kafka.controller.ControllerChannelManager=INFO sensitive=false synonyms={} kafka.log.TimeIndex=INFO sensitive=false synonyms={}Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change the logging level for one or more broker loggers. Use the
--alterand--add-configoptions and specify each logger and its level as a comma-separated list in double quotes./opt/kafka/bin/kafka-configs.sh --bootstrap-server <broker_address> --alter --add-config "LOGGER-ONE=NEW-LEVEL,LOGGER-TWO=NEW-LEVEL" --entity-type broker-loggers --entity-name BROKER-ID
/opt/kafka/bin/kafka-configs.sh --bootstrap-server <broker_address> --alter --add-config "LOGGER-ONE=NEW-LEVEL,LOGGER-TWO=NEW-LEVEL" --entity-type broker-loggers --entity-name BROKER-IDCopy to Clipboard Copied! Toggle word wrap Toggle overflow For example, for broker
0:/opt/kafka/bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --add-config "kafka.controller.ControllerChannelManager=WARN,kafka.log.TimeIndex=WARN" --entity-type broker-loggers --entity-name 0
/opt/kafka/bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --add-config "kafka.controller.ControllerChannelManager=WARN,kafka.log.TimeIndex=WARN" --entity-type broker-loggers --entity-name 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow If successful this returns:
Completed updating config for broker: 0.
Completed updating config for broker: 0.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Resetting a broker logger
You can reset one or more broker loggers to their default logging levels by using the kafka-configs.sh tool. Use the --alter and --delete-config options and specify each broker logger as a comma-separated list in double quotes:
/opt/kafka/bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --delete-config "LOGGER-ONE,LOGGER-TWO" --entity-type broker-loggers --entity-name BROKER-ID
/opt/kafka/bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --delete-config "LOGGER-ONE,LOGGER-TWO" --entity-type broker-loggers --entity-name BROKER-ID
12.3. Dynamically change logging levels for Kafka Connect and MirrorMaker 2 Copy linkLink copied to clipboard!
Dynamically change logging levels for Kafka Connect workers or MirrorMaker 2 connectors at runtime without having to restart.
Use the Kafka Connect API to change the log level temporarily for a worker or connector logger. The Kafka Connect API provides an admin/loggers endpoint to get or modify logging levels. When you change the log level using the API, the logger configuration in the connect-log4j.properties configuration file does not change. If required, you can permanently change the logging levels in the configuration file.
You can only change the logging level of MirrorMaker 2 at runtime when in distributed or standalone mode. Dedicated MirrorMaker 2 clusters have no Kafka Connect REST API, so changing the logging level is not possible.
The default listener for the Kafka Connect API is on port 8083, which is used in this procedure. You can change or add more listeners, and also enable TLS authentication, using admin.listeners configuration.
Example listener configuration for the admin endpoint
admin.listeners=https://localhost:8083 admin.listeners.https.ssl.truststore.location=/path/to/truststore.jks admin.listeners.https.ssl.truststore.password=123456 admin.listeners.https.ssl.keystore.location=/path/to/keystore.jks admin.listeners.https.ssl.keystore.password=123456
admin.listeners=https://localhost:8083
admin.listeners.https.ssl.truststore.location=/path/to/truststore.jks
admin.listeners.https.ssl.truststore.password=123456
admin.listeners.https.ssl.keystore.location=/path/to/keystore.jks
admin.listeners.https.ssl.keystore.password=123456
If you do not want the admin endpoint to be available, you can disable it in the configuration by specifying an empty string.
Example listener configuration to disable the admin endpoint
admin.listeners=
admin.listeners=
Prerequisites
- Streams for Apache Kafka is installed on each host, and the configuration files are available.
- Kafka is running.
- Kafka Connect or MirrorMaker 2 is running.
Procedure
Switch to the
kafkauser:su - kafka
su - kafkaCopy to Clipboard Copied! Toggle word wrap Toggle overflow Check the current logging level for the loggers configured in the
connect-log4j.propertiesfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use a curl command to check the logging levels from the
admin/loggersendpoint of the Kafka Connect API:Copy to Clipboard Copied! Toggle word wrap Toggle overflow jqprints the output in JSON format. The list shows standardorgandrootlevel loggers, plus any specific loggers with modified logging levels.If you configure TLS (Transport Layer Security) authentication for the
admin.listenersconfiguration in Kafka Connect, then the address of the loggers endpoint is the value specified foradmin.listenerswith the protocol as https, such ashttps://localhost:8083.You can also get the log level of a specific logger:
curl -s http://localhost:8083/admin/loggers/org.apache.kafka.connect.mirror.MirrorCheckpointConnector | jq { "level": "INFO" }curl -s http://localhost:8083/admin/loggers/org.apache.kafka.connect.mirror.MirrorCheckpointConnector | jq { "level": "INFO" }Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use a PUT method to change the log level for a logger:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you change the
rootlogger, the logging level for loggers that used the root logging level by default are also changed.