Periodically cut the Tomcat log under Linux and delete the log record before a specified number of days

  • 2020-06-19 12:17:00
  • OfStack

System. out and System. err are both printed to catalina. out. catalina.out not rotate. catalina. out files tend to get larger and larger after deployment, which has a significant impact on system stability.

1. The conf/ logging. properties log profile can be modified to mask this part of log information.


[root@localhost conf]# pwd
/usr/local/tomcat/conf
[root@localhost conf]# cp logging.properties logging.propertiesbak
[root@localhost conf]# vim logging.properties
 25 1catalina.org.apache.juli.FileHandler.level = FINE
 26 1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
 27 1catalina.org.apache.juli.FileHandler.prefix = catalina.

Setting the level level to WARNING can significantly reduce log output, or you can set it to OFF and disable it.
1 General log levels are:


SEVERE (highest value) > WARNING > INFO > CONFIG > FINE > FINER > FINEST (lowest value)

2. Use cronolog tool to shard catalina.ES31en log files of Tomcat

Download and install cronolog


[root@localhost src]# rpm -qa |grep cronolog
[root@localhost src]# tar zxvf cronolog-1.6.2.tar.gz 
[root@localhost src]# cd cronolog-1.6.2
[root@localhost cronolog-1.6.2]# ./configure 
[root@localhost cronolog-1.6.2]# make && make install
[root@localhost cronolog-1.6.2]# which cronolog
/usr/local/sbin/cronolog
[root@localhost cronolog-1.6.2]#

With which cronolog you can find the installation path, which will be used later when modifying ES40en.sh.

Change catalina.sh changes 183 lines to 184, comments out 355 lines, replaces 368 and 369 lines with 370 and 371 lines, replaces 379 and 380 lines with 381 and 382 lines;


[root@localhost cronolog-1.6.2]# cp /usr/local/tomcat/bin/catalina.sh /usr/local/tomcat/bin/catalina.shbak
[root@localhost cronolog-1.6.2]# vim /usr/local/tomcat/bin/catalina.sh
182 if [ -z "$CATALINA_OUT" ] ; then
183 # CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
184  CATALINA_OUT="$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out
185 fi
......
355 # touch "$CATALINA_OUT"
......
363    -Djava.security.manager \
364    -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
365    -Dcatalina.base="$CATALINA_BASE" \
366    -Dcatalina.home="$CATALINA_HOME" \
367    -Djava.io.tmpdir="$CATALINA_TMPDIR" \
368 #   org.apache.catalina.startup.Bootstrap "$@" start \
369 #   >> "$CATALINA_OUT" 2>&1 &
370   org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \ 
371   | /usr/local/sbin/cronolog "$CATALINA_OUT" >> /dev/null &
372 
373  else
374   "$_RUNJAVA" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
375    -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
376    -Dcatalina.base="$CATALINA_BASE" \
377    -Dcatalina.home="$CATALINA_HOME" \
378    -Djava.io.tmpdir="$CATALINA_TMPDIR" \
379 #   org.apache.catalina.startup.Bootstrap "$@" start \
380 #   >> "$CATALINA_OUT" 2>&1 &
381    org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \ 
382    | /usr/local/sbin/cronolog "$CATALINA_OUT" >> /dev/null &
383 
384  fi
[root@localhost bin]# ./catalina.sh start
Using CATALINA_BASE:  /app/apache-tomcat-7.0.61
Using CATALINA_HOME:  /app/apache-tomcat-7.0.61
Using CATALINA_TMPDIR: /app/apache-tomcat-7.0.61/temp
Using JRE_HOME:    /app/jdk1.7.0_79
Using CLASSPATH:    /app/apache-tomcat-7.0.61/bin/bootstrap.jar:/app/apache-tomcat-7.0.61/bin/tomcat-juli.jar
Tomcat started.
[root@localhost bin]# service tomcat stop
[root@localhost bin]# service tomcat start

You can check the configuration files by./ catalina.sh start; In/usr local tomcat/logs catalina will automatically generate a day. % Y m - - % % d. The out file, here we need to do is regularly cleaning out these files, we can be done through crontab


[root@localhost logs]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost logs]# crontab -l
30 5 * * 6 /bin/find /usr/local/tomcat/logs/ -mtime +7 -type f -name "catalina.*.out" -exec /bin/rm -f {} \;
[root@localhost logs]# cat /var/spool/cron/root 
30 5 * * 6 /bin/find /usr/local/tomcat/logs/ -mtime +7 -type f -name "catalina.*.out" -exec /bin/rm -f {} \;
[root@localhost logs]#

Date format string:


%a        The local name of the week is short (e.g.: Sun..Sat)  
%A       Local full week name (e.g.: Sunday .. Saturday)  
%b       Local short month name (e.g.: Jan .. Dec)  
%B       Local full month name (e.g.: January .. December)  
%c        Local date and time (e.g.:  " Sun Dec 15 14:12:47 GMT 1996 " )  
%d      1 What day of the month (01 .. 31)  
%j       1 Day of the year  (001 .. 366)  
%m       The numerical representation of the month name  (01 .. 12)  
%U      1 The week ends on Sunday in the middle of the year 1 Days are the days of the week (00..53,  The first 1 The week includes the end of the New Year 1 On a Sunday )  
%W      1 Midyear week 1 A week for the first 1 Days are the days of the week (00..53,  The first 1 The week includes the end of the New Year 1 A few weeks 1)  
%w       The number of the day of the week  (0 .. 6, 0 For the Sunday )  
%x        Local date  (e.g.  It is today in Beijing :  " 15/12/96 " )  
%y        Years without centuries (00 .. 99)  
%Y        Years with centuries (1970 .. 2038)

Time format string:


%H        24 Hour by hour (00..23)  
%I        12 Hour by hour (01..12)  
%p         local AM/PM indicator   
%M        minutes (00..59)  
%S         seconds (00..61)  
%X        Local time (e.g.:  " 15:12:47 " )  
%Z        The time zone  (e.g. GMT) If the time zone cannot be detected, the value is null 

Special format string:


%%        % character   
%n         A new row   
%t         tab character 

conclusion


Related articles: