1、下载(最新版本)
# wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
2、解压缩
tar -xvf cronolog-1.6.2.tar
3、进入cronolog安装文件所在目录
cd cronolog-1.6.2
4、运行安装
# ./configure
# make
# make install
5、查看cronolog安装后所在目录(验证安装是否成功)
# which cronolog
一般情况下显示为:/usr/local/sbin/cronolog
6、进入Tomcat的bin目录,打开catalina.sh文件
6.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
6.2、
将:
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 \
-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 $_NOHUP "\"$_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
修改为:
shift
#touch "$CATALINA_OUT"
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
6.3、配置JVM参数
JAVA_OPTS="$JAVA_OPTS -server -Xss512k -Xms8g -Xmx8g -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=1024m -XX:+UseG1GC -XX:MaxGCPauseMillis=10 -XX:InitiatingHeapOccupancyPercent=65 -XX:NewRatio=2 -XX:SurvivorRatio=12 -XX:G1ReservePercent=10 -XX:ConcGCThreads=8 -XX:ParallelGCThreads=8 -Xloggc:gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+UnlockExperimentalVMOptions -XX:G1LogLevel=finest -XX:+PrintReferenceGC -XX:+PrintAdaptiveSizePolicy"
注:视服务器配置和应用做对应的修改
7、shell利用crontab自动清除日志
命令:find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \;
例子:
find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} \;
说明:将/opt/soft/log/目录下所有30天前带".log"的文件删除。具体参数说明如下:
find:linux的查找命令,用户查找指定条件的文件;
/opt/soft/log/:想要进行清理的任意目录;
-mtime:标准语句写法;
+30:查找30天前的文件,这里用数字代表天数;
"*.log":希望查找的数据类型,"*.jpg"表示查找扩展名为jpg的所有文件,"*"表示查找所有文件,这个可以灵活运用,举一反三;
-exec:固定写法;
rm -rf:强制删除文件,包括目录;
{} \; :固定写法,一对大括号+空格+\+;
将脚本使用crontab部署于服务器即可