zookeeper为啥不使用我的log4j.properties文件日志目录

Posted

技术标签:

【中文标题】zookeeper为啥不使用我的log4j.properties文件日志目录【英文标题】:Why does zookeeper not use my log4j.properties file log directoryzookeeper为什么不使用我的log4j.properties文件日志目录 【发布时间】:2014-12-24 03:03:59 【问题描述】:

在我的 zookeeper/conf/log4j.properties 文件中,我将 zookeeper.log.dir 设置为 $HOME/zklogs

当我使用 zkServer.sh 时,它不使用该目录。相反,它使用 $ZOO_LOG_DIR ,当我回显它时,它会出现在“。”

我不明白如何解决这个问题,我在任何地方都没有看到 $ZOO_LOG_DIR 设置。我不确定它是如何设置为“。”一点也不。我也不知道如何在没有 zkServer.sh 的情况下启动 zookeeper。我对 linux 也很陌生,在这个问题上有点迷茫......

有人知道我该如何解决这个问题,以便它使用我在 conf 目录中的 log4j.properties 文件中设置的目录吗?

***更新,我在我的 zookeeper 安装的 bin 目录中找到了 zkEnv.sh。有代码:

if["x$ZOO_LOG_DIR" = "x" ]
then
   ZOO_LOG_DIR="."
fi

我不确定第一行发生了什么,但一定是这里出了点问题。我希望它从我的 log4j.properties 文件中查看 zookeeper.log.dir。谁能告诉我这是否应该是真的?我不想在这里硬连线路径...

【问题讨论】:

【参考方案1】:

也许不是寻求的答案,但是对于 conf 目录中的 zookeeper-3.4.11,您可以在文件 log4j.properties 中设置适当的级别和配置

【讨论】:

【参考方案2】:

zkServer.shzkEnv.sh 获取环境变量 如果 log4j 文件位于预期位置,则 env 文件会设置一个包含 log4j 文件的类路径。

ZOOXFGDIR=ZOOBINDIR/../conf

我在 zkServer.sh 中加入了一些回声来追踪发生了什么。 我发现类路径设置正确,但 logdir 和 log4j_prop 没有设置。所以我将它们添加到zkEnv.sh。日志现在似乎显示在预期位置。

ZOO_LOG_DIR="/var/log/zookeeper"
ZOO_LOG4J_PROP="INFO,ROLLINGFILE"

【讨论】:

【参考方案3】:

通过 ZOO_LOG_DIR 环境变量更改文件日志目录的另一个选项是创建自己的 zk-server-1/conf/zookeeper-env.sh。

阅读 zookeeper-3.4.6 dist bash 脚本(t.i. zkEnv.sh),他们获取 zookeeper-env.sh 文件(如果存在)。这是(我创建的)内容:

export ZOO_HOME=$ZOOKEEPER_PREFIX
echo $ZOO_HOME
export ZOO_LOG_DIR=$ZOO_HOME/logs

我习惯有一个 PRODUCTNAME_HOME 变量,它指向 zookeeper 的***安装目录。这个***目录似乎被 zookeeper dist 命名为 $ZOOKEEPER_PREFIX,我将其命名为 $ZOO_HOME 并将其用于 ZOO_LOG_DIR 环境变量。

在这篇文章的最后,我列出了 bash 脚本调试信息 - 帮助我调查使用了哪些环境或局部变量加值。

如我所见,/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf 目录位于 CLASSPATH 上,因此 log4j.properties 文件符合条件用于通过 java 的 log4j 框架的类加载器加载资源(这是 log4j AFAIR 的实现方式)。

joma@kopernikus-u:~/dev/programme/zk-local-ensemble$ bash -x zk-server-1/bin/zkServer.sh start
+ '[' x = x ']'
+ JMXLOCALONLY=false
+ '[' x = x ']'
+ echo 'JMX enabled by default'
JMX enabled by default
+ ZOOMAIN='-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain'
+ ZOOBIN=zk-server-1/bin/zkServer.sh
++ dirname zk-server-1/bin/zkServer.sh
+ ZOOBIN=zk-server-1/bin
++ cd zk-server-1/bin
++ pwd
+ ZOOBINDIR=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin
+ '[' -e zk-server-1/bin/../libexec/zkEnv.sh ']'
+ . /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/zkEnv.sh
++ ZOOBINDIR=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin
++ ZOOKEEPER_PREFIX=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/..
++ '[' x = x ']'
++ '[' -e /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf ']'
++ ZOOCFGDIR=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf
++ '[' -f /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf/zookeeper-env.sh ']'
++ . /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf/zookeeper-env.sh
+++ export ZOO_HOME=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/..
+++ ZOO_HOME=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/..
+++ echo /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/..
/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/..
+++ export ZOO_LOG_DIR=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../logs
+++ ZOO_LOG_DIR=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../logs
++ '[' x = x ']'
++ ZOOCFG=zoo.cfg
++ ZOOCFG=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf/zoo.cfg
++ '[' -f /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf/java.env ']'
++ '[' x/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../logs = x ']'
++ '[' x = x ']'
++ ZOO_LOG4J_PROP=INFO,CONSOLE
++ '[' /home/joma/entwicklung/programme/jdk1.7.0_51 '!=' '' ']'
++ JAVA=/home/joma/entwicklung/programme/jdk1.7.0_51/bin/java
++ CLASSPATH=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:
++ for i in '"$ZOOBINDIR"/../src/java/lib/*.jar'
++ CLASSPATH='/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:'
++ '[' -e '/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../share/zookeeper/zookeeper-*.jar' ']'
++ for i in '"$ZOOBINDIR"/../zookeeper-*.jar'
++ CLASSPATH='/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../zookeeper-3.4.6.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:'
++ LIBPATH=("$ZOOBINDIR"/../lib/*.jar)
++ for i in '"$LIBPATH[@]"'
++ CLASSPATH='/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/jline-0.9.94.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../zookeeper-3.4.6.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:'
++ for i in '"$LIBPATH[@]"'
++ CLASSPATH='/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/log4j-1.2.16.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/jline-0.9.94.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../zookeeper-3.4.6.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:'
++ for i in '"$LIBPATH[@]"'
++ CLASSPATH='/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/netty-3.7.0.Final.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/log4j-1.2.16.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/jline-0.9.94.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../zookeeper-3.4.6.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:'
++ for i in '"$LIBPATH[@]"'
++ CLASSPATH='/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/slf4j-api-1.6.1.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/netty-3.7.0.Final.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/log4j-1.2.16.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/jline-0.9.94.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../zookeeper-3.4.6.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:'
++ for i in '"$LIBPATH[@]"'
++ CLASSPATH='/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/slf4j-api-1.6.1.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/netty-3.7.0.Final.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/log4j-1.2.16.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/jline-0.9.94.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../zookeeper-3.4.6.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:'
++ for d in '"$ZOOBINDIR"/../build/lib/*.jar'
++ CLASSPATH='/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../build/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/slf4j-api-1.6.1.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/netty-3.7.0.Final.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/log4j-1.2.16.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/jline-0.9.94.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../zookeeper-3.4.6.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:'
++ CLASSPATH='/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../build/classes:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../build/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/slf4j-api-1.6.1.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/netty-3.7.0.Final.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/log4j-1.2.16.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/jline-0.9.94.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../zookeeper-3.4.6.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:'
++ case "`uname`" in
+++ uname
++ cygwin=false
++ false
+ '[' x '!=' x ']'
+ '[' x '!=' x ']'
++ dirname /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf/zoo.cfg
+ '[' x/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf '!=' x/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf ']'
+ false
+ KILL=kill
+ echo 'Using config: /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf/zoo.cfg'
Using config: /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf/zoo.cfg
+ '[' -z '' ']'
++ grep '^[[:space:]]*dataDir' /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf/zoo.cfg
++ sed -e 's/.*=//'
+ ZOO_DATADIR=/home/joma/dev/programme/zk-local-ensemble/data/zk1
+ '[' '!' -d /home/joma/dev/programme/zk-local-ensemble/data/zk1 ']'
+ ZOOPIDFILE=/home/joma/dev/programme/zk-local-ensemble/data/zk1/zookeeper_server.pid
+ '[' '!' -w /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../logs ']'
+ _ZOO_DAEMON_OUT=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../logs/zookeeper.out
+ case $1 in
+ echo -n 'Starting zookeeper ... '
Starting zookeeper ... + '[' -f /home/joma/dev/programme/zk-local-ensemble/data/zk1/zookeeper_server.pid ']'
+ '[' 0 -eq 0 ']'
+ /bin/echo -n 24744
+ nohup /home/joma/entwicklung/programme/jdk1.7.0_51/bin/java -Dzookeeper.log.dir=/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../logs -Dzookeeper.root.logger=INFO,CONSOLE -cp '/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../build/classes:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../build/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/slf4j-api-1.6.1.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/netty-3.7.0.Final.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/log4j-1.2.16.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../lib/jline-0.9.94.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../zookeeper-3.4.6.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../src/java/lib/*.jar:/home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf:' -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf/zoo.cfg
+ sleep 1
+ echo STARTED
STARTED

【讨论】:

顺便说一句,作为软件的用户和开发人员,我完全喜欢“wahtthebliepisgoingonhereshowmethelogs”。 zookeeper 应该将他们的 bash err/sysout 通过管道传输到一些 boot.log 中,直到 log4j 启​​动。并准备标准 log4j.properties 以将 err/sysout 重定向到文件 appender。 BTW2,并将标准 log4j.properties 文件 appender 置于 append 模式!【参考方案4】:

我想补充一下我是如何解决这个问题的/自定义我的环境的。

这里有两种日志机制:

bin/zkServer.sh 将zookeeper服务器的stdout和stderr重定向到zookeeper.out log4j 可以将日志附加到多个位置,包括: CONSOLE - 最终出现在 zookeeper 服务器的 stdout 和 stderr 中 ROLLINGFILE - 发送到 zookeeper.log

bin/zkServer.sh 使用:

ZOO_LOG_DIR 为 zookeeper.out 和 log4j 设置路径。 ZOO_LOG4J_PROP 设置 log4j 日志记录级别以及打开哪些日志附加程序

设置 conf/log4j.properties 中的“最终”默认值由 zookeeper bash 脚本组合设置:

ZOO_LOG_DIR = . (启动 zookeeper 的工作目录) 在 conf/log4j.properties 中设置为 zookeeper.log.dir ZOO_LOG4J_PROP = 信息,控制台 在 conf/log4j.properties 中设置为 zookeeper.root.logger

打开日志附加器 CONSOLE 的效果是日志现在转到标准输出。因为 bin/zkServer.sh 将 stdout 和 stderr 重定向到 zookeeper.out,所以 log4j 日志最终在 zookeeper.out 中。关闭ROLLINGFILE的效果是没有创建zookeeper.log文件。

zookeeper.out 日志没有轮换。 zookeeper.log 日志设置为轮换,可以设置为过期旧日志。

我希望日志滚动并过期。我必须做的第一件事是更改 conf/log4j.properties 以导致旧日志过期/删除。我通过在 conf/log4j.properties 中设置 log4j.appender.ROLLINGFILE.MaxBackupIndex 来做到这一点。我要做的第二件事是设置日志目录、日志级别和附加程序。

我有一个每分钟运行一次的 bash 脚本。如果它看到 zookeeper 没有运行,它就会运行:

bin/zkServer.sh start

我将其更改为指定 bin/zkServer.sh 预期的环境变量。

sudo ZOO_LOG_DIR=/opt/zookeeper-3.4.6/logs ZOO_LOG4J_PROP='INFO,ROLLINGFILE' /opt/zookeeper-3.4.6/bin/zkServer.sh start

关闭日志附加器 CONSOLE 的效果是 log4j 日志现在不再以 zookeeper.out 结尾。开启ROLLINGFILE的效果是zookeeper.log文件被创建、轮换、过期。

顺便说一句,conf/log4j.properties 显然已经在我的类路径中。在这方面我不必做任何改变。

这条链对我的理解做出了重大贡献: https://groups.google.com/forum/#!msg/nosql-databases/aebIvNnT0xY/doky1X9-WfwJ

【讨论】:

@Jeff Mass:- 我在***.com/questions/57336317/… 提到了一个类似的问题,唯一的区别是,由于我是从我的主课开始 ZK,你能帮我吗?【参考方案5】:

据我所知,zkEnv.sh zookeeper/conf 文件夹已经在类路径中。有两个问题:

    zkServer.sh 添加“-Dzookeeper.log.dir=”。到 ZK 启动命令,除非你指定 ZOO_LOG_DIR 环境变量

    即使你确实指定了 ZOO_LOG_DIR,ZK 可能仍然无法找到你的 log4j.properties 文件,因为 zkEnv.sh 是这样指定 CLASSPATH 的:

    $CLASSPATH="$ZOOCFGDIR:$CLASSPATH"

如果您的 $CLASSPATH 环境变量不为空,这可以正常工作,但是当它为空时,它只会留下一个尾随冒号,这会破坏您的类路径。 我将上面的行更改为:

#add the zoocfg dir to classpath
if [ "x$CLASSPATH" = "x" ]
then
    CLASSPATH="$ZOOCFGDIR"
else
    CLASSPATH="$ZOOCFGDIR:$CLASSPATH"
fi

并且还为 ZK 传递了 ZOO_LOG_DIR 环境,因为 zkServer.sh 在启动 ZK 时会生成 -Dzookeeper.log.dir VM 参数。这是为我启动 ZooKeeper 的命令,使其读取 /opt/zookeeper/conf/log4j.properties 并将日志文件保存在 /opt/zimbra/log 中:

ZOO_LOG_DIR=/opt/zimbra/log /opt/zookeeper/bin/zkServer.sh start

【讨论】:

【参考方案6】:

我相信我的问题的答案是我需要将该 log4j.properties 文件移动到我的类路径中,然后 zkEnv.sh 才会读取我在调用 zkServer.sh 时在 log4j.properties 中设置的值.我相信我只是不明白如何正确使用 log4j。

【讨论】:

以上是关于zookeeper为啥不使用我的log4j.properties文件日志目录的主要内容,如果未能解决你的问题,请参考以下文章

为啥dubbo使用ZkClient作为zookeeper的客户端

为啥dubbo使用zkclient作为zookeeper的客户端

dubbo在Github上为啥不更新了

为啥我的Maven引入spring-cloud-starter-zookeeper-all报红?

为啥搭建Kafka需要zookeeper

为啥Kafka需要zookeeper