SoFunction
Updated on 2025-03-08

Three ways to automatically segment Tomcat logs

Tomcat before modification saves all data in a log file. There is no problem in the experimental environment. However, in the production environment, due to the huge amount of data, it will be difficult to view the logs. Therefore, the logs are automatically divided through certain methods. The modified tomcat will save the log files according to "date". The file format for example: catalina.

1. Method 1: Use cronolog

Official website:

RPM package download method: Search for cronolog on the cronolog and select the corresponding RPM package copy download link

Install cronolog on the lower halberd:

[root@localhost ~]# yum install /linux/epel/7/x86_64/Packages/c/cronolog-1.6.2-14.el7.x86_64.rpm -y

Check the directory after the cronolog is installed (verify that the installation is successful):

[root@localhost logs]# whereis cronolog
cronolog: /usr/sbin/cronolog /usr/share/man/man1/cronolog.

Edit the bin/ file (tomcat startup file):

1.Find the line below and use it#Comment outtouch "$CATALINA_BASE"/logs/
In new Tomcat7 of bin/ 文件要Note释of行是
touch "SCATALINA_OUT"

2.替换下面of行(There are two places,But generally-security 中of那一行不需要去关Note,You might as well replace both places):
 "$@" start \
    >> "$CATALINA_BASE"/logs/ 2>&1 &
Modified to
 "$@" start \
2>&1 | /usr/local/sbin/cronolog "$CATALINA_BASE" /logs/catalina-%Y-%m-%>>/dex/null &

In new Tomcat7 of bin/ 中需要替换of行是
>> "$CATALINA_OUT" 2>&1 "&"
替换后该行of内容与上面是一样of

Note:Why Tomcat7 Will be different afterward,因为在它of  The following definitions are found in the file
CATALINA_OUT="$CATALINA_BASE" /logs/

3.save  Restart the file Tomcat Just
[root@localhost ~]# systemctl restart 

Check the log segmentation results:

[root@localhost ~]# ll -d /usr/local/tomcat/logs/
-rw-r--r-- 1 tomcat tomcat 106331 Jun 10 16:51 /usr/local/tomcat/logs/
# You can see that it already exists

2. Method 2: Use rotatelogs

`rotatelogg` is a tool that comes with `apache`

Check which package rotatelogs belongs to (confirm whether it is included in apache):

# Through observation, confirm that rotatelegs is included in the apache package, so you only need to install httpd to install rotatelegs
[root@localhost logs]# yum whatprovides */rotatelogs
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: 
 * extras: 
 * updates: 
httpd-2.4.6-95..x86_64 : Apache HTTP Server
Repo        : base
Matched from:
Filename    : /usr/sbin/rotatelogs

httpd-2.4.6-97..x86_64 : Apache HTTP Server
Repo        : updates
Matched from:
Filename    : /usr/sbin/rotatelogs

httpd-2.4.6-97..1.x86_64 : Apache HTTP Server
Repo        : updates
Matched from:
Filename    : /usr/sbin/rotatelogs

httpd-2.4.6-97..2.x86_64 : Apache HTTP Server
Repo        : updates
Matched from:
Filename    : /usr/sbin/rotatelogs

httpd-2.4.6-97..4.x86_64 : Apache HTTP Server
Repo        : updates
Matched from:
Filename    : /usr/sbin/rotatelogs

httpd-2.4.6-97..5.x86_64 : Apache HTTP Server
Repo        : updates
Matched from:
Filename    : /usr/sbin/rotatelogs

Install httpd:

[root@localhost ~]# yum install httpd -y

Check the directory after rotatelogs is installed (verify that the installation is successful):

[root@localhost ~]# whereis rotatelogs
rotatelogs: /usr/sbin/rotatelogs /usr/share/man/man8/rotatelogs.

Edit the bin/ file (tomcat startup file):

1.Find the line below and use it#Comment outtouch "$CATALINA_BASE"/logs/
In new Tomcat7 of bin/ 文件要Note释of行是
touch "SCATALINA_OUT"

2.替换下面of行(There are two places,But generally-security 中of那一行不需要去关Note,You might as well replace both places):
 "$@" start \
    >> "$CATALINA_BASE"/logs/ 2>&1 &
Modified to
 "$@" start \
2>&1 | /usr/sbin/rotatelogs "$CATALINA_BASE"/logs/catalina-%Y%m% 86400 480 &
#To distinguish from the above, in the name format of the log here, the year, month and day of the date are not separated by "-"
In new Tomcat7 of bin/ 中需要替换of行是
>> "$CATALINA_OUT" 2>&1 "&"
替换后该行of内容与上面是一样of

Note:Why Tomcat7 Will be different afterward,因为在它of  The following definitions are found in the file
CATALINA_OUT="$CATALINA_BASE" /logs/

3.save  Restart the file Tomcat Just
[root@localhost ~]# systemctl restart 

Check the log segmentation results:

[root@localhost ~]# ll -d /usr/local/tomcat/logs/
-rw-r--r-- 1 tomcat tomcat 105380 Jun 10 17:22 /usr/local/tomcat/logs/
# You can see that it already exists (to distinguish the log format, the year, month and day of the date are not separated by "-", and there are mentioned above)

3. Method 3: Use scripts

Cutting log scripts in Linux environment

Put this script in tomcat Next bin Table of contents,exist crontabAutomatic execution in,shellThe script is as follows:
#!/bin/bash
cd `dirname $0`
d=`date +%Y-%m-%d`
d7=`date -d'7 day ago' +%Y-%m-%d'

cd ../logs/

cp  .${d}
echo "" > 
rm -rf .${d7}

I won't make a demonstration here, if you need to test it yourself

This is the end of this article about the three methods of automatic segmentation of Tomcat logs. For more related content on automatic segmentation of Tomcat logs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!