龙空技术网

生产环境tomcat日志切割

狂飙会飞的鱼 581

前言:

如今各位老铁们对“apachehostmanager”大致比较重视,同学们都需要分析一些“apachehostmanager”的相关资讯。那么小编同时在网摘上收集了一些有关“apachehostmanager””的相关知识,希望同学们能喜欢,朋友们快快来学习一下吧!

▶日志改进

(1)对已发生时间的存量日志压缩保留

(2)记录catalina.out、localhost_access_log日志

(3)catalina.out将按天分割,即每天生成一份当日日志,更名为catalina.out.yyyy-MM-dd.out。此日志保留15天,15天外的日志使用定时任务执行shell脚本方式压缩存档。

▶安装必要环境

yum -y install epel-releaseyum install -y cronolog gzip

▶修改配置文件

1.tomcat家目录下bin/catalina.sh

#touch "$CATALINA_OUT"           //注释掉  if [ "$1" = "-security" ] ; then    if [ $have_tty -eq 1 ]; then      echo "Using Security Manager"    fi    shift    eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \      -D$ENDORSED_PROP="\"$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/sbin/cronolog "$CATALINA_BASE"/logs/catalina.out.%Y-%m-%d.out>> /dev/null &  else    eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \      -D$ENDORSED_PROP="\"$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/sbin/cronolog "$CATALINA_BASE"/logs/catalina.out.%Y-%m-%d.out>> /dev/null &  fi

2.修改{$tomcat_home}/conf/logging.properties(不再记录localhost、manager等日志)

找到以下内容,对其进行注释。

1catalina.org.apache.juli.AsyncFileHandler.level = FINE1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-82localhost.org.apache.juli.AsyncFileHandler.level = FINE2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-83manager.org.apache.juli.AsyncFileHandler.level = FINE3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs3manager.org.apache.juli.AsyncFileHandler.prefix = manager.3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-84host-manager.org.apache.juli.AsyncFileHandler.level = FINE4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8

▶日志归集备份脚本

#!/bin/bash#此脚本应存放于${tomcat_home}目录,将eight_days_ago时间外的日志压缩到${tomcat_home}/logs/logbak中echo `date '+%Y-%m-%d %H:%M:%S'` "压缩备份开始" >> /www/tomcat/logs/logbak/compresslog.logwork_path_real=$(cd $(dirname $0) && pwd )work_path=$work_path_real/logsecho "脚本目录:"${work_path_real} eight_days_ago=`date -d '16 days ago' +%Y-%m-%d`echo "工作目录:" $work_path, "备份时间:" $eight_days_ago if [ -d $work_path ]then        #压缩日志        cd $work_path 	if [ -f catalina.out.${eight_days_ago}.out ] && [ -f localhost_access_log.${eight_days_ago}.txt ]	then        	tar -czf  logbak/catalina.out.${eight_days_ago}.out.tar.gz  catalina.out.${eight_days_ago}.out localhost_access_log.${eight_days_ago}.txt  		rm -f  catalina.out.${eight_days_ago}.out localhost_access_log.${eight_days_ago}.txt 		echo "catalina.out.${eight_days_ago}.out.tar.gz 备份完成..." >> /www/tomcat/logs/logbak/compresslog.log	else		echo "catalina.out.${eight_days_ago}.out.tar.gz 备份出错..." >> /www/tomcat/logs/logbak/compresslog.log	fi       else        echo "tomcat logs directory is not exist" >> /www/tomcat/logs/logbak/compresslog.logfiecho `date "+%Y-%m-%d %H:%M:%S"`  "备份结束" >> /www/tomcat/logs/logbak/compresslog.log

▶计划任务

30 23 * * * /bin/bash /www/tomcat/compresslog.sh >> /www/tomcat/logs/logbak/compresslog.log 2>&1

标签: #apachehostmanager