异常描述

在测试ZK连接服务器的时候控制一直没有其他输出,log4j日志提示下方信息:

log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

解决思路

  • 需要我们添加初始化 log4j.properties 配置文件
log4j.rootLogger=INFO,console,dailyFile
# TODO 发布到阿里云记得添加,另外控制台不输出(只输出warn或者error信息)

# log4j.logger.org.mybatis = INFO
log4j.logger.com.imooc.mapper=INFO

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.encoding=UTF-8
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n

# 定期滚动日志文件,每天都会生成日志
log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyFile.encoding=UTF-8
log4j.appender.dailyFile.Threshold=INFO
# TODO 本地日志地址,正式环境请务必切换为阿里云地址
log4j.appender.dailyFile.File=C:/logs/maven-ssm-alipay/log.log4j
log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd
log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n
  • 重新运行我们的程序,log 正常输出显示了
2019-01-24 09:51:39,537 [main] [com.chandler.NativeDemo.zk.ZKConnect.main(ZKConnect.java:27)] - [WARN] 客户端开始连接zookeeper服务器...
2019-01-24 09:51:39,539 [main] [com.chandler.NativeDemo.zk.ZKConnect.main(ZKConnect.java:28)] - [WARN] 连接状态:CONNECTING
2019-01-24 09:51:39,540 [main-SendThread(127.0.0.1:2181)] [org.apache.zookeeper.ClientCnxn$SendThread.logStartConnect(ClientCnxn.java:1035)] - [INFO] Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2019-01-24 09:51:39,579 [main-SendThread(127.0.0.1:2181)] [org.apache.zookeeper.ClientCnxn$SendThread.primeConnection(ClientCnxn.java:877)] - [INFO] Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2019-01-24 09:51:40,124 [main-SendThread(127.0.0.1:2181)] [org.apache.zookeeper.ClientCnxn$SendThread.onConnected(ClientCnxn.java:1302)] - [INFO] Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x16879c6acc1000a, negotiated timeout = 5000
2019-01-24 09:51:40,126 [main-EventThread] [com.chandler.NativeDemo.zk.ZKConnect.process(ZKConnect.java:38)] - [WARN] 接收到watch通知:WatchedEvent state:SyncConnected type:None path:null
2019-01-24 09:51:41,539 [main] [com.chandler.NativeDemo.zk.ZKConnect.main(ZKConnect.java:32)] - [WARN] 连接状态:CONNECTED
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐