Tomecat本身日志优化
1、使得只生成catalina.out
2、自动按日切割压缩打包日志(tar.gz)
3、删除15天前的旧文件
4、使用cronolog定时执行该脚本
步骤一:删除webapps下的原始文件,修改conf/logging.properties文件
#2localhost.org.apache.juli.FileHandler.level = FINE
#2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
#2localhost.org.apache.juli.FileHandler.prefix = localhost.
#3manager.org.apache.juli.FileHandler.level = FINE
#3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
#3manager.org.apache.juli.FileHandler.prefix = manager.
#4host-manager.org.apache.juli.FileHandler.level = FINE
#4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
#4host-manager.org.apache.juli.FileHandler.prefix = host-manager.
修改conf/server.xml文件 注释掉下面的一段话
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
步骤二: 编写shell脚本catalina-cut.sh,放在tomcat的bin目录下,并赋予执行权限 chmod +x catalina-cut.sh
#!/bin/bash
DIR=$(cd $(dirname ${BASH_SOURCE[0]}); pwd )
echo $DIR/../logs
d=`date +%Y%m%d%H`
cd $DIR/../logs
cp catalina.out catalina.out.${d}
echo "" > catalina.out
tar -czvf catalina.out.${d}.tar.gz catalina.out.${d}
rm -rf catalina.out.${d}
find $DIR/../logs -type f -mtime +15 -name "*.log" -exec rm -f {} ;
find $DIR/../logs -type f -mtime +15 -name "*.tar.gz" -exec rm -f {} ;
## 步骤三:安装cronolog ##
#下载cronolog
wget https://files.cnblogs.com/files/crazyzero/cronolog-1.6.2.tar.gz
#解压缩
tar -zxvf cronolog-1.6.2.tar.gz
#进入目录
cd cronolog-1.6.2
#列出所有文件
ll
#configure,若不想自定义安装路径则直接./configure
./configure --prefix=/usr/local/cronolog
#若发现缺很多包,安装之,否则不用管
yum install gcc cc aclocal autoconf automake autoheader makeinfo
#若前面出现缺编译环境,并已经安装了编译环境,则再次configure,否则不用管,若不想自定义安装路径则直接./configure
./configure --prefix=/usr/local/cronolog
#make
make
#安装
make install
#检查/usr/local/cronolog下是否有文件,如果有结果则安装成功
cd /usr/local/cronolog
ll
#若不是自定义路径安装的使用下面命令看,如果有结果则安装成功
which cronolog
步骤四:配置cronolos,定时执行脚本
crontab -e #按i 写入下面的内容 :w 保存内容
50 23 * * * /root/dev/tomcat-8.5.53-elite/bin/catalina-cut.sh
表示每天23点50分执行目录/root/dev/tomcat-8.5.53-elite/bin下的catalina-cut.sh脚本