Search code examples
tomcatlogging

Tomcat localhost_access_log files cleanup


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?


Solution

  • 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 rotatable="true" by default you should already have it. Then you can delete old logs via an external script. For example to delete logs older than 10 days:

    Unix:

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

    Windows:

    C:\> forfiles /p "C:\path\to\httplogs" /s /m *.log /d -10 /c "cmd /c del @PATH"