7

We have Solr running on Tomcat 8. We are having issues in our different environments with localhost_access_log files filling up the servers. These files are created by the Access Valve Log in server.xml configured like this -

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
   prefix="localhost_access_log" 
   suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />

From what I've read, there is no OOTB way in Tomcat to clean up old log files. What can I implement to clean up the old access log files?

John Eipe
  • 10,922
  • 24
  • 72
  • 114
Gabbar
  • 4,006
  • 7
  • 41
  • 78

3 Answers3

8

In theory you don't have to do it manually. Set this property in your config/server.xml and the server will clean for you automatically.

maxDays="10"

Example configuration line:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
   prefix="localhost_access_log" 
   suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b"
   maxDays="10" />

Then restart your tomcat / tomcat8 / tomcat9 service.

Valerio Bozz
  • 1,176
  • 16
  • 32
vsingh
  • 6,365
  • 3
  • 53
  • 57
  • 3
    Please make sure to use double quotes around the value for maxDays i.e. maxDays="10", otherwise Tomcat will thrown an error when trying to parse the value. – abhishek Dec 29 '19 at 10:37
7

You can have a log rotation and then choose what logs files to delete

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" rotatable="true" renameOnRotate="true" pattern="%h %l %u %t &quot;%r&quot; %s %b" />

As rotation is set to true by default you should already have it. Then you can for exemple delete logs older than 5 days:

To delete log files older than 10 days the following commands can be used.

Unix
find /path/to/httplogs/ -name "*.log" -type f -mtime +10 -exec rm -f {} \;

For Windows Server OS:
forfiles /p "C:\path\to\httplogs" /s /m *.log /d -10 /c "cmd /c del @PATH"
Community
  • 1
  • 1
Erwan C.
  • 709
  • 5
  • 18
2

You can disable localhost_access log by commenting configuration line.

or

In linux, set daily cron job to delete old files.

0 0 * * * /path/to/your/script/cleanup.sh

cleanup.sh

#This will remove files older than a week.
find /TOMCAT_HOME/logs -name "localhost_access_log*.txt" -type f -mtime +7 -exec rm -f {} \;
Community
  • 1
  • 1
Darshan Patel
  • 2,839
  • 2
  • 25
  • 38
  • I implemented a cron to delete the access logs but the problem is the tomcat does not release memory until it is restarted. – dgarg Jul 17 '21 at 18:21