Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Kafka logging with log4j

I downloaded kafka-clients-0.9.0.0.jar with maven and i expect i would see logging like those in this link Kafka Logging

However i have no idea why i am not getting any logging, even I set the bootstrap.servers wrongly on purpuse, but it just got stuck without throwing any warning. I added a few lines of code to print to a file using log4j and it seems work but no idea why Kafka cannot log event to log4j.

import org.apache.log4j.Logger;
public class ConsumerLoop implements Runnable {
    final static Logger logger = Logger.getLogger(ConsumerLoop.class);

    @Override
    public void run() {

        logger.warn("running!!!!!");
        }
}


SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2016-04-04 12:54:01 WARN  ConsumerLoop:40 - running!!!!!

Note, there is slf4j-api-1.7.6.jar that came as a dependency of kafka. Even I included the required library slf4j-api-x.x.x.jar, slf4j-log4jx-x.x.x.jar and used slf4j to log even but still cannot get the kafka logs.

like image 519
MartinOverflow Avatar asked Apr 04 '16 04:04

MartinOverflow


People also ask

Does Kafka use log4j?

Kafka Connect and other Confluent Platform components use the Java-based logging utility Apache Log4j to collect runtime data and record component events.

What version of log4j does Kafka use?

The following components in Apache Kafka use Log4j-v1. 2.17 : broker, controller, zookeeper, connect, mirrormaker and tools. Clients may also be configured to use Log4j-v1. x .


Video Answer


1 Answers

This is an old question but someone may still benefit.

Just put log4j.properties under /src/main/resources with data

log4j.rootLogger=TRACE, stdout    
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d] %p %m (%c)%n

#log4j.appender.fileAppender=org.apache.log4j.FileAppender
#log4j.appender.fileAppender.File=kafka-request.log
#log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
#log4j.appender.fileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n


# Turn on all our debugging info
log4j.logger.kafka=TRACE,stdout
#log4j.logger.kafka.producer.async.DefaultEventHandler=DEBUG,stdout
#log4j.logger.kafka.consumer.PartitionTopicInfo=TRACE,stdout
#log4j.logger.kafka.request.logger=TRACE,fileAppender
#log4j.additivity.kafka.request.logger=false
#log4j.logger.kafka.network.Processor=TRACE,fileAppender
#log4j.additivity.kafka.network.Processor=false
#log4j.logger.org.I0Itec.zkclient.ZkClient=DEBUG

And add dependency

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>

Then you can see logs in console.

like image 196
Burak Avatar answered Oct 04 '22 02:10

Burak