Tomcat – Application logs can’t be cleared without stopping

log-filesloggingtomcat

I have some Tomcats where the application log (the one with [tomcat name].[hostname].[node].[webapp].log name format) can't be cleared unless I stop the Tomcat and clear the log. The log size turns zero for a moment but the log soon returns to its original size. Even when the log is shown as zeroed, the disk usage remains the same.

This is causing disk space issues, since the archive script archives the log but can't clear it.

Why does it happen and what can I do to solve this? Stopping the Tomcats is not a possibility, since the company's services are 24/7.

Best Answer

There are a couple of possible answers to your question. A common approach and probably the easiest solution to solve your problem would be to introduce log rotation:

logging.properties

According to your description I assume you are talking about the "standard" logging configuration typically placed in <TOMCAT_HOME>/conf/logging.properties

Example for the manager-application:

3manager.org.apache.juli.FileHandler.maxDays = 3

Your archiving script now could pick up the "old" files. More information on Tomcat logging in the Tomcat documentation

Valve component

Another configuration possibility (depending on your exact demands) is to modify the Valve component found in <TOMCAT_HOME>/conf/server.xml

Example configuration per host:

<Host name="example.host.domain" appBase="webapp_example" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="webapp_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" maxDays=3 /> </Host>

Log4j

Another way could be to use DailyRollingFileAppender or RollingFileAppender in Log4j where you can also define maxBackupIndex to delete old files automatically.

catalina.out

If you need to rotate the standard "catalina.out"-file as well, you can find a description in the FAQ