使用 cronolog 分割 tomcat7 的 catalina.out 日志

时间:2022-01-12 07:47:41

tomcat日志catalina.out默认不会分割,随着运行时间越来越长,文件越来越大对于查询是个很大的麻烦,所以使用cronolog工具对其进行分割。

1. 下载cronolog
cronolog-1.6.2.tar.gz

2. 安装cronolog
# tar -zxvf cronolog-1.6.2.tar.gz
# cd cronolog-1.6.2
# ./configure
# make && make install

3. 修改tomcat的catalina.sh文件
修改如下:
1)将:
if [ -z "$CATALINA_OUT" ] ; then
 CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
fi
改为:
if [ -z "$CATALINA_OUT" ] ; then
 CATALINA_OUT="$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out
fi

2)注释 touch "$CATALINA_OUT",约370行
# touch "$CATALINA_OUT"

3)将:
 if [ "$1" = "-security" ] ; then
 if [ $have_tty -eq 1 ]; then
 echo "Using Security Manager"
 fi
 shift
 eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
 -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
 -Djava.security.manager \
 -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
 -Dcatalina.base="\"$CATALINA_BASE\"" \
 -Dcatalina.home="\"$CATALINA_HOME\"" \
 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
 org.apache.catalina.startup.Bootstrap "$@" start \
 >> "$CATALINA_OUT" 2>&1 "&"

 else
 eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
 -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
 -Dcatalina.base="\"$CATALINA_BASE\"" \
 -Dcatalina.home="\"$CATALINA_HOME\"" \
 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
 org.apache.catalina.startup.Bootstrap "$@" start \
 >> "$CATALINA_OUT" 2>&1 "&"

 fi

改为:
 if [ "$1" = "-security" ] ; then
 if [ $have_tty -eq 1 ]; then
 echo "Using Security Manager"
 fi
 shift
 eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
 -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
 -Djava.security.manager \
 -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
 -Dcatalina.base="\"$CATALINA_BASE\"" \
 -Dcatalina.home="\"$CATALINA_HOME\"" \
 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
 org.apache.catalina.startup.Bootstrap "$@" start \
 2>&1 |/usr/local/sbin/cronolog "$CATALINA_OUT" &

 else
 eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
 -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
 -Dcatalina.base="\"$CATALINA_BASE\"" \
 -Dcatalina.home="\"$CATALINA_HOME\"" \
 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
 org.apache.catalina.startup.Bootstrap "$@" start \
 2>&1 |/usr/local/sbin/cronolog "$CATALINA_OUT" &

 fi

修改完成,保存退出。