Tomcat 日志切割之cronolog

1 安装cronolog

[root@app_51 ~]$ yum install -y cronolog
[root@app_51 ~]$ which cronolog
/usr/sbin/cronolog

2 修改tomcat的catalina.sh

[ghl@app_51 ~]$ cd /home/ghl/tomcat/apache-tomcat-8.5.37/bin/
[ghl@app_51 bin]$ cp catalina.sh catalina.sh.bak
#需要将第453-491的代码,直接替换成下面的
shift
 #touch "$CATALINA_OUT"
 if [ "$1" = "-security" ] ; then
   if [ $have_tty -eq 1 ]; then
     echo "Using Security Manager"
   fi
   shift
   "$_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/sbin/cronolog "$CATALINA_BASE"/logs/catalina.out.%Y-%m-%d.out >> /dev/null &

 else
   "$_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/sbin/cronolog "$CATALINA_BASE"/logs/catalina.out.%Y-%m-%d.out >> /dev/null &

 fi

3 查看原来的catalina

Tomcat 日志切割之cronolog

[ghl@app_51 bin]$ sed -n "453,491P" catalina.sh.bak
 shift
 if [ -z "$CATALINA_OUT_CMD" ] ; then
   touch "$CATALINA_OUT"
   catalina_out_command=">> \"$CATALINA_OUT\" 2>&1"
 else
   catalina_out_command="| $CATALINA_OUT_CMD"
 fi
 if [ ! -z "$CATALINA_PID" ]; then
   catalina_pid_file="$CATALINA_PID"
 else
   catalina_pid_file=/dev/null
 fi
 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 \& echo \$! \>\"$catalina_pid_file\" \; \} $catalina_out_command "&"

 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 \& echo \$! \>\"$catalina_pid_file\" \; \} $catalina_out_command "&"

 fi

4 查看修改后的

Tomcat 日志切割之cronolog

[ghl@app_51 bin]$ sed -n "453,491P" catalina.sh
 shift
  #touch "$CATALINA_OUT"
  if [ "$1" = "-security" ] ; then
    if [ $have_tty -eq 1 ]; then
      echo "Using Security Manager"
    fi
    shift
    "$_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/sbin/cronolog "$CATALINA_BASE"/logs/catalina.out.%Y-%m-%d.out >> /dev/null &

  else
    "$_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/sbin/cronolog "$CATALINA_BASE"/logs/catalina.out.%Y-%m-%d.out >> /dev/null &

  fi

  echo "Tomcat started."

 elif [ "$1" = "stop" ] ; then

  shift

  SLEEP=5
  if [ ! -z "$1" ]; then
    echo $1 | grep "[^0-9]" >/dev/null 2>&1
    if [ $? -gt 0 ]; then
      SLEEP=$1
      shift
    fi
[ghl@app_51 bin]$ 

5 启动tomcat后查看日志格式

[ghl@app_51 bin]$ ./startup.sh 
 Using CATALINA_BASE:   /home/ghl/tomcat/apache-tomcat-8.5.37
 Using CATALINA_HOME:   /home/ghl/tomcat/apache-tomcat-8.5.37
 Using CATALINA_TMPDIR: /home/ghl/tomcat/apache-tomcat-8.5.37/temp
 Using JRE_HOME:        /usr/local/jdk1.8.0_144/
 Using CLASSPATH:       /home/ghl/tomcat/apache-tomcat-8.5.37/bin/bootstrap.jar:/home/ghl/tomcat/apache-tomcat-8.5.37/bin/tomcat-juli.jar
 Tomcat started.
[ghl@app_51 bin]$ netstat -tnlp|grep 8080|8009
 Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name                   
 tcp6       0      0 :::8009                 :::*                    LISTEN      5408/java           
 tcp6       0      0 :::8080                 :::*                    LISTEN      5408/java           
[ghl@app_51 apache-tomcat-8.5.37]$ cd logs/
[ghl@app_51 logs]$ ll
总用量 36
-rw-r----- 1 ghl ghl 11399 7月   7 01:06 catalina.2019-07-07.log
-rw-r----- 1 ghl ghl  6102 7月   7 00:24 catalina.out
-rw-r----- 1 ghl ghl  8409 7月   7 01:06 catalina.out.2019-07-07.out
-rw-r----- 1 ghl ghl     0 7月   7 00:21 host-manager.2019-07-07.log
-rw-r----- 1 ghl ghl     0 7月   7 00:21 localhost.2019-07-07.log
-rw-r----- 1 ghl ghl   147 7月   7 00:21 localhost_access_log.2019-07-07.txt
21-rw-r----- 1 ghl ghl     0 7月   7 00:21 manager.2019-07-07.log
上一篇:共享文件夹权限的备份和恢复


下一篇:as3文档中根元素后面的标记格式必须正确