Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat localhost_access_log files cleanup

Tags:

logging

tomcat

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?

like image 932
Gabbar Avatar asked Aug 07 '15 20:08

Gabbar


People also ask

Can I delete Tomcat log files?

You can delete all logs. Those from the current date will not be deletable while Tomcat is running, but that is ok.

Where are Tomcat logs stored?

The main Apache Tomcat configuration file is at /opt/bitnami/tomcat/conf/server. xml. Once Apache Tomcat starts, it will create several log files in the /opt/bitnami/tomcat/logs directory.

How do you empty Catalina?

After stopping the tomcat, you can clear out the contents under Logs folder of tomcat directory. There is a file called logging. properties located under tomcatdirectory\conf\ which has the logging level set. You can change the logging from there as well.

What is Catalina out file in Tomcat?

The catalina.out log messages and log files communicate events and conditions that affect Tomcat server's operations. Logs for all identity applications components including OSP and Identity Reporting are also logged to the catalina.out file.


Video Answer


3 Answers

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.

like image 52
vsingh Avatar answered Nov 15 '22 09:11

vsingh


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"
like image 40
Erwan C. Avatar answered Nov 15 '22 11:11

Erwan C.


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 {} \;
like image 41
Darshan Patel Avatar answered Nov 15 '22 09:11

Darshan Patel