How to delete Tomcat Access Log after n days?
Asked Answered
A

8

24

I only would like to keep the Access Logs of the last n days created by Tomcat Access Log Valve. http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html#Access%20Log%20Valve

But there seems to be no configuration-Attribute to define how long to keep the log-files? I guess this is because "Access Log Valve" only creates log files and doesn't delete them, is that correct?

Advisable answered 5/5, 2010 at 9:40 Comment(0)
A
17

By default rotatable is true for Access Log, so you will be having a new file created every 24 hours.

Tomcat itself does not do any housekeeping on the old files, the general principle on a Unix system is to have a cron job set up on the system to archive older files into a back up directory and/or delete them.

Aphrodisia answered 5/5, 2010 at 10:18 Comment(5)
Thank you for the answer. You confirmed my assumption, that files need to be deleted by a cronjob (or a visual basic script on windows).Advisable
How do you remove the old logs ? I did try to do it on modify time, but in some cases the log files have been modified by other process / users, and as such not removed in time ...Cutting
Preety old but, leaving the answer to the question if someone runs into this at some point. #8962977Azevedo
This is not completely correct. Catalina supports a maxDays parameter. tomcat.apache.org/tomcat-7.0-doc/logging.htmlHammett
@ValerioBozz Not true for acces logs - maxDays cannot be used there, because it does not use log4j for logging.Atharvaveda
H
5

For Tomcat 7 you can config tomcat/conf/logging.properties. Example:

1catalina.org.apache.juli.FileHandler.maxDays = 90

Note: that 1 is not a typo.

https://tomcat.apache.org/tomcat-7.0-doc/logging.html

Note: this does not affect access log files, which are handled by the Access Log Valve, not by the main Tomcat (Apache Commons) logging.

Haha answered 20/12, 2018 at 1:50 Comment(2)
This does not affect access log files, which are handled by the Access Log Valve, not by the main Tomcat (Apache Commons) logging.Afterglow
Thank you @SteveHart, I've updated the answer with your useful note.Hammett
O
4

For Windows, based on Erwan's answer in Tomcat localhost_access_log files cleanup, for the given folder and recursing into all subfolders:

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

To test, just use:

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

And when having multiple suffixes in the log folder, like both .txt and .log, see using FORFILES in batch to delete tmp and bak files older than a week:

for %%t in (.txt, .log) do forfiles /p "C:\path\to\httplogs\" /s /m *%%t /d -10 /c "cmd /c del @PATH"
Organization answered 11/8, 2016 at 9:10 Comment(0)
P
3

You can try to create logrotate config:

#cat /etc/logrotate.d/tomcat
/var/log/tomcat/*.log {
        su tomcat tomcat
        copytruncate  
        daily  
        rotate 6  
        compress  
        missingok
}

"su tomcat tomcat" - i added for avoiding logrotate error on wrong permissions

Punishment answered 19/11, 2015 at 14:28 Comment(0)
F
2

Incase of Apache Tomcat 7.0. You can use maxDays parameter to delete old log files. https://tomcat.apache.org/tomcat-7.0-doc/config/valve.html

Faustina answered 19/6, 2018 at 17:30 Comment(0)
F
2

For tomcat 9 its part of access log configuration. See http://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Access_Logging:

maxDays
The maximum number of days rotated access logs will be retained for before being deleted. If not specified, the default value of -1 will be used which means never delete old files.

Fawcett answered 13/5, 2021 at 14:39 Comment(0)
M
1

If you use tomcat within spring boot you can use server.tomcat.accesslog.max-days=3 to delete old files after e. g. 3 days.

A minimal working configuration with spring boot to log the accesslog would be:

server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=C:/path/to/dir
server.tomcat.accesslog.max-days=3

References:

Microhenry answered 18/2, 2022 at 11:10 Comment(0)
T
0

run in terminal:

locate RELEASE-NOTES | egrep 'tomcat|apache' | xargs grep "Apache Tomcat Version"
Tail answered 17/9, 2020 at 1:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.