Kafka源码工程examples项目配置log4j

Posted 李志涛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kafka源码工程examples项目配置log4j相关的知识,希望对你有一定的参考价值。

examples项目启动想知道有哪些错误,通过日志了解代码执行逻辑,但是启动SimpleConsumerDemo了报错如下:

log4j.proproties也配置了

log4j.proproties配置如下:

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
log4j.rootLogger=DEBUG, stdout, log

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

log4j.logger.kafka=DEBUG
log4j.logger.org.apache.kafka=DEBUG

### log file ###
log4j.appender.log = org.apache.log4j.DailyRollingFileAppender
log4j.appender.log.File = /Users/lizhitao/code/kafka-0.11.0.2-src/examples/logs/kafka.log
log4j.appender.log.Append = true
log4j.appender.log.Threshold = DEBUG
log4j.appender.log.DatePattern=\'.\'yyyy-MM-dd
log4j.appender.log.layout = org.apache.log4j.PatternLayout
log4j.appender.log.layout.ConversionPattern = [kafka][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n


# zkclient can be verbose, during debugging it is common to adjust is separately
log4j.logger.org.I0Itec.zkclient.ZkClient=WARN
log4j.logger.org.apache.zookeeper=WARN
 

 

No appenders could be found for logger,意思是log4.proproties找不到,如果默认位置找不到,我们可以自行动态配置

generateData()方法加入如下log4j配置代码:

PropertyConfigurator.configure("/Users/lizhitao/code/xxx-proj/kafka-0.11.0.2-src/examples/src/main/resources/log4j.properties");

再次启动SimpleConsumerDemo,搞定,日志有输出

以上是关于Kafka源码工程examples项目配置log4j的主要内容,如果未能解决你的问题,请参考以下文章

Log4j日志在java项目中的使用(附工程源码)

log4j2发送消息至Kafka

Log4j配置文件整理--Kafka日志发送

log4j2(一) 获取 ILoggerFactory

如何为 Kafka 生产者配置日志记录?

log4j2日志发给kafka