首页 > 代码库 > Zookeeper 日志输出到指定文件夹

Zookeeper 日志输出到指定文件夹

最近在研究Zookeeper Storm Kafka, 顺便在本地搭了一套集群, 遇到了Zookeeper日志问题输出路径的问题, 发现zookeeper设置log4j.properties不能解决日志路径问题, 发现解决方案如下:

1. 修改log4j.properties, 这个大家都应该会改, 红色加粗处是我修改的, 但是改了这边还是不生效

# Define some default values that can be overridden by system propertieszookeeper.root.logger=INFO,ROLLINGFILE zookeeper.console.threshold=INFOzookeeper.log.dir=.zookeeper.log.file=zookeeper-1.logzookeeper.log.threshold=DEBUGzookeeper.tracelog.dir=.zookeeper.tracelog.file=zookeeper_trace.log## ZooKeeper Logging Configuration## Format is "<default threshold> (, <appender>)+# DEFAULT: console appender onlylog4j.rootLogger=${zookeeper.root.logger}# Example with rolling log file#log4j.rootLogger=DEBUG, CONSOLE, ROLLINGFILE# Example with rolling log file and tracing#log4j.rootLogger=TRACE, CONSOLE, ROLLINGFILE, TRACEFILE## Log INFO level and above messages to the console#log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n## Add ROLLINGFILE to rootLogger to get log file output#    Log DEBUG level and above messages to a log filelog4j.appender.ROLLINGFILE=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}# Max log file size of 10MB#log4j.appender.ROLLINGFILE.MaxFileSize=10MB# uncomment the next line to limit number of backup files#log4j.appender.ROLLINGFILE.MaxBackupIndex=10log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n## Add TRACEFILE to rootLogger to get log file output#    Log DEBUG level and above messages to a log filelog4j.appender.TRACEFILE=org.apache.log4j.FileAppenderlog4j.appender.TRACEFILE.Threshold=TRACElog4j.appender.TRACEFILE.File=${zookeeper.tracelog.dir}/${zookeeper.tracelog.file}log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout### Notice we are including log4js NDC here (%x)log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L][%x] - %m%n

 

2. 还需要改${zkhome}/bin/zkEnv.sh, 请留意红色加粗处, 这时日志已经可以成功按照你设置的目录进行输出了

#!/usr/bin/env bash# 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.# This script should be sourced into other zookeeper# scripts to setup the env variables# We use ZOOCFGDIR if defined,# otherwise we use /etc/zookeeper# or the conf directory that is# a sibling of this scripts directoryZOOBINDIR="${ZOOBINDIR:-/usr/bin}"ZOOKEEPER_PREFIX="${ZOOBINDIR}/.."if [ "x$ZOOCFGDIR" = "x" ]then  if [ -e "${ZOOKEEPER_PREFIX}/conf" ]; then    ZOOCFGDIR="$ZOOBINDIR/../conf"  else    ZOOCFGDIR="$ZOOBINDIR/../etc/zookeeper"  fifiif [ -f "${ZOOCFGDIR}/zookeeper-env.sh" ]; then  . "${ZOOCFGDIR}/zookeeper-env.sh"fiif [ "x$ZOOCFG" = "x" ]then    ZOOCFG="zoo.cfg"fiZOOCFG="$ZOOCFGDIR/$ZOOCFG"if [ -f "$ZOOCFGDIR/java.env" ]then    . "$ZOOCFGDIR/java.env"fiif [ "x${ZOO_LOG_DIR}" = "x" ]then    ZOO_LOG_DIR="/apps/logs/zookeeper"fiif [ "x${ZOO_LOG4J_PROP}" = "x" ]then    ZOO_LOG4J_PROP="INFO,ROLLINGFILE"fiif [ "$JAVA_HOME" != "" ]; then  JAVA="$JAVA_HOME/bin/java"else  JAVA=javafi#add the zoocfg dir to classpathCLASSPATH="$ZOOCFGDIR:$CLASSPATH"for i in "$ZOOBINDIR"/../src/java/lib/*.jardo    CLASSPATH="$i:$CLASSPATH"done#make it work in the binary package#(use array for LIBPATH to account for spaces within wildcard expansion)if [ -e "${ZOOKEEPER_PREFIX}"/share/zookeeper/zookeeper-*.jar ]; then  LIBPATH=("${ZOOKEEPER_PREFIX}"/share/zookeeper/*.jar)else  #release tarball format  for i in "$ZOOBINDIR"/../zookeeper-*.jar  do    CLASSPATH="$i:$CLASSPATH"  done  LIBPATH=("${ZOOBINDIR}"/../lib/*.jar)fifor i in "${LIBPATH[@]}"do    CLASSPATH="$i:$CLASSPATH"done#make it work for developersfor d in "$ZOOBINDIR"/../build/lib/*.jardo   CLASSPATH="$d:$CLASSPATH"done#make it work for developersCLASSPATH="$ZOOBINDIR/../build/classes:$CLASSPATH"case "`uname`" in    CYGWIN*) cygwin=true ;;    *) cygwin=false ;;esacif $cygwinthen    CLASSPATH=`cygpath -wp "$CLASSPATH"`fi#echo "CLASSPATH=$CLASSPATH"

3. 美中不足的是在你设定的目录中, 仍会有zookeeper.out文件存在, 虽然它的size=0, 但是仍让我感到不爽.

究其原因是因为zkServer.sh会使用nohup进行zookeeper的启动, 然而nohup必然会输出一个日志文件到你设置的目录中,

相关代码如下, 需要将此处的逻辑修改掉, 就可以将zookeeper.out移除啦, 如果你不是处女座当然可以省略这一步

 

_ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper.out"case $1 instart)    echo  -n "Starting zookeeper ... "    if [ -f "$ZOOPIDFILE" ]; then      if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then         echo $command already running as process `cat "$ZOOPIDFILE"`.         exit 0      fi    fi    nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}"     -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &

 

 

 

到此为止, zookeeper的日志设置就到此结束, 谢谢!

 

Zookeeper 日志输出到指定文件夹