In my zookeeper/conf/log4j.properties file I set the zookeeper.log.dir to $HOME/zklogs
When I use zkServer.sh it does not use that directory. Instead it uses the ${ZOO_LOG_DIR} which when I echo it, comes out to "."
I don't understand how fix this issue, I don't see the ${ZOO_LOG_DIR} set anywhere. I am not sure how it gets set to "." at all. I also don't know how to launch zookeeper without zkServer.sh. I am noobish at linux too and a little lost on this issue...
Does anybody know how I can fix this issue so that it uses the directory set in my log4j.properties file in the conf directory?
***UPDATE, I found in zkEnv.sh in the bin directory of my zookeeper install. There is code:
if["x${ZOO_LOG_DIR}" = "x" ]
then
ZOO_LOG_DIR="."
fi
I am not sure what is going on in that first line, but it has to be here that something is going wrong. I expect it to look at zookeeper.log.dir from my log4j.properties file. Can anybody tell me if that should be true? I don't want to just hardwire the path here...
zkServer.sh
gets it's environment variables fromzkEnv.sh
The env file sets a classpath which includes the log4j file if it's at the expected location.ZOOXFGDIR=ZOOBINDIR/../conf
I dropped some echos into zkServer.sh to trace what's going on. I found that classpath was being set properly, but logdir and log4j_prop were not being set. So I added them to
zkEnv.sh
. Logs appear to be showing up in the expected location now.I believe the answer to my question is that I need to move that log4j.properties file into my classpath and then and only then will zkEnv.sh read of the values that I set in log4j.properties when I call zkServer.sh. I believe I just did not understand how to use log4j properly.
Another option to change the file log directory via the ZOO_LOG_DIR environment variable is to create your own zk-server-1/conf/zookeeper-env.sh.
Reading the zookeeper-3.4.6 dist bash scripts(t.i. zkEnv.sh), they source that zookeeper-env.sh file if it exists. Here is it's(my created) content:
I am used to having a PRODUCTNAME_HOME variable, which points to the top installation dir of zookeeper. This top dir seems to be named $ZOOKEEPER_PREFIX by zookeeper dist, which i aliase with $ZOO_HOME und use it for the ZOO_LOG_DIR environment variable.
At the end of this post i spooled out the bash scripts debug info - helps me investigate which environment or local variables plus values are used.
As i see it, the /home/joma/dev/programme/zk-local-ensemble/zk-server-1/bin/../conf dir is on the CLASSPATH, hence the log4j.properties file is eligible for resource loading via java's class-loader for the log4j framework(which is the way log4j is implemented AFAIR).
I wanted to add how I fixed this problem / customized my environment.
There are 2 logging mechanisms working here:
bin/zkServer.sh uses :
The "eventual" defaults in the setup conf/log4j.properties are set by a combination of zookeeper bash scripts:
The effect of turning on the log appender CONSOLE is that logs now go to stdout. Because bin/zkServer.sh redirects stdout and stderr to zookeeper.out, log4j logs end up in zookeeper.out. The effect of turning off ROLLINGFILE is that no zookeeper.log file is created.
The zookeeper.out log is not rotated. The zookeeper.log log is set to be rotated and can be set to expire old logs.
I wanted logs to roll and be expired. The first thing I had to do was change conf/log4j.properties to cause the expiration/deletion of old logs. I did that by setting log4j.appender.ROLLINGFILE.MaxBackupIndex inside of conf/log4j.properties. The second thing I had to do was set the log directory, logging level and appenders.
I have a bash script that runs every minute. If it sees that zookeeper isn't running, it runs :
I changed it to specify environmental variables expected by bin/zkServer.sh.
The effect of turning off the log appender CONSOLE is that log4j logs now no longer end up in zookeeper.out. The effect of turning on ROLLINGFILE is that zookeeper.log file is created, rotated, and expired.
BTW, conf/log4j.properties was apparently already in my classpath. I had to make no changes in that regard.
This chain contributed significantly to my understanding: https://groups.google.com/forum/#!msg/nosql-databases/aebIvNnT0xY/doky1X9-WfwJ
As far as I can see from zkEnv.sh zookeeper/conf folder is already in the classpath. There are two problems there:
even if you do specify ZOO_LOG_DIR, ZK may still be unable to find your log4j.properties file, because zkEnv.sh specifies CLASSPATH like this:
$CLASSPATH="$ZOOCFGDIR:$CLASSPATH"
this works fine if your $CLASSPATH environment variable is not empty, however when it is empty, it just leaves a trailing colon, which screws up your classpath. I changed the lines above to this:
and also passed ZOO_LOG_DIR environment for ZK, because zkServer.sh generates -Dzookeeper.log.dir VM argument when starting up ZK. Here is the command that starts ZooKeeper for me, makes it read /opt/zookeeper/conf/log4j.properties and keep log files in /opt/zimbra/log:
Maybe not the sought answer, but for zookeeper-3.4.11 in the conf directory you can set the appropriate levels and configuration in the file log4j.properties