SoFunction
Updated on 2025-03-08

Tomcat solves the problem of excessive file size

Preface

If you use Tomcat, you will definitely encounter this problem: the file is too large.

It is the log file generated by Tomcat by default. It will gradually grow and continue to grow over time, even reaching a size of several G or dozens of G. Because the files are too large, not only occupy the system's storage, we will not be able to use regular editing tools to view them, which seriously affects the system's maintenance work.

In this regard, the following solutions to solve the problem of excessive files have emerged.

Violent

Unless logs are not required,

1. Manual version

Each time the hard disk space of tomcat is monitored to decrease and reaches the threshold, manually log in to the server, switch to tomcat logs, and manually clear it

echo " "  > 

2. Script version

Write scripts, put them in scheduled tasks, and clear them regularly

crontab -e 
0 24 * * *    sh /root/qin_catalina.

vim qin_catalina.
 #!/usr/bin/bash 
 echo " " > 

Technical

1. Log Cutting Tool Edition 0.1

Use the cronolog log segmentation tool to split Tomcat's log files

<1>Download cronolog and install

wget /download/cronolog-1.6.   (Chinese servers may not be downloaded or downloaded slowly,You can download it to the overseas server first)

tar zxvf cronolog-1.6.

./cronolog-1.6.2/configure

make

make install

(Installed by default in/usr/local/sbinDown)

<2>.Configuration

in tomcat/bin/

 "$@" start \ &gt;&gt; "$CATALINA_BASE"/logs/ 2&amp;1 &amp;

Change to:
"$@" start \ |/usr/local/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-% &gt;&gt; /dev/null 2&gt;&amp;1 &amp;
or
   "$@"  start  2&gt;&amp;1  \   |  /usr/local/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-% &gt;&gt; /dev/null &amp; 

And comment    touch "$CATALINA_OUT"  

<3>Restart Tomcat

Tomcat output log file is successfully divided, and the output log file format becomes: catalina.

1. Log Cutting Tool Edition 0.2

The logrotate program after CentOS6.5 can solve the problem of log rotation

<1> Create a new tomcat file in the /etc// directory

cat &gt;/etc//tomcat 

/usr/local/tomcat/logs/{            Files to be rotated
    copytruncate                                Create a new copy,Truncate the source file
    daily                                       Daily document rotation
    rotate 7                                    Retain at most7A copy
    missingok                                   Files are missing,No errors in rotation
    compress                                    Using compression
    size 500M                                    When the file is larger than16MB,Just turn
}

Other parameters:

compress                                                                                                                                                                         �
nocompress
copytruncate
nocopytruncate
create mode owner group                                                                                                                                                                       �
nocreate
delaycompress
nodelaycompress                                           Overwrite the delaycompress option and dump it while compressing.
Missingok
errors address
ifempty
notifempty                                                                                                                                                                       �
mail address
nomail
olddir directory                                                                                                                                                                       �
noolddir                                                                                                                                                                       �
sharedscripts                                                                                                                                                                         If this is not configured, the script will be executed once after each log rotation
prerotate                                                                                                                                                                       �
postrotate                                                                                                                                                                       � Must be independent
daily
weekly
monthly
rotate count
dateext
dateformat .%s
size (or minsize) log-size                                 dumped when the log file reaches the specified size. log-size can specify bytes (default) and KB (sizek) or

<2> When the above operations are performed automatically, it can also be manually cut

logrotate /etc/

If you only rotate the tomcat configuration file, you need to specify the file

logrotate --force /etc//tomcat

<3>Delete the log to be cleaned

Manually search for log files that need to be cleaned

cd /usr/local/tomcat/logs
rm -rf .

2. Log cutting script version

Use cron to regularly backup the current one every day and then clear his content;

<1>crontab -e

01 0 * * *  sh /root/qie_catalina.

<2>cat qie_catalina. Reference script

 #!/bin/bash 
 DATE=`date "+%Y-%m-%d"`

cp /etc/tomcat/logs/  /etc/tomcat/logs/`.$DATE`
wait

echo " " > 

3. Modify log level version

Log Level:

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

<1>Modify the conf/log configuration file and set the level level to WARNING to reduce the log output. It can also be set to OFF and directly disabled.

 = WARNING

 = ${}/logs

 = catalina.

This is the end of this article about Tomcat solving the problem of excessive file size. For more related Tomcat files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!