I have a Tomcat server which create access logs via a valve (org.apache.catalina.valves.FastCommonAccessLogValve). This valve takes care of rotating the access log file, but not of compressing them or deleting them after some time.
For the moment, I have a cron job that uses find [...] -mtime +30 [...]
to compress and delete the logs. I would rather use logrotate, so that log rotation is in a centralized place for all logs. I dont like having a seperate solution just for Tomcat.
I tried to read logrotate documentation, but I am still a bit lost. Can I use logrotate just to compress and cleanup log files ? How would I do that ?
Or turning the problem around, is there a Tomcat access log valve that will compress and cleanup log files ?
Thank for your help !
Fairly simple one that i have seen works like this.
Create a file in /etc/logrotate.d called tomcat containing the following:-
This runs daily, compresses the file, and keeps 7 days worth (rotate 7). copytruncate means that it'll copy then truncate the original file so that there is no need to restart tomcat. missing ok will not error if it's not there.
The access.log Valve can be changed to not rotate by adding rotatable=false:-
Modified TimP's script--Added deleting very old files, added scan for old compressed files.
It's surprisingly simple. Just tell logrotate which files you specifically want to rotate.
nocreate
tells logrotate not to recreate an empty file after moving the old one (if you're rotating files into a subfolder).I did not wish to change the Tomcat configuration, so created a script which compressed the rotated files
For local_access_log.YYYY-MM-DD.txt compression I wrote this script after seeing this post :-
A simple solution provided here. This also includes the s3 upload option.
https://yottabrain.org/tomcat/tomcat-log-backup/