ARTIFACTORY: How to reduce the amount of tomcat logs in 7.x with logrotate
Currently in 7.x, there is a $ARTIFACTORY_HOME/var/log/tomcat/ folder that gets all the tomcat logs. This creates daily tomcat-catalina-<date>.log and tomcat-catalina-<date>.log files. In some setups this can create too many files or be annoying to manage. There are ways to customize the logging here though. In this article, we’ll be merging the daily logs into a single log file and rotating it into the $ARTIFACTORY_HOME/var/log/archived/ folder with all of the other rotated application logs.
Step 1: Consolidating the daily tomcat logsSo first, merging the daily logs. To do so, we go to the
$ARTIFACTORY_HOME/app/artifactory/tomcat/conf/logging.properties
file. Here we will add the following two lines
1catalina.org.apache.juli.FileHandler.rotatable=false 2localhost.org.apache.juli.FileHandler.rotatable=false
which will prevent the rotation, as well as stop printing the date on the log. I also recommend to remove the ending hyphen (or rename it completely) in the
FileHandler.prefix=tomcat-[localhost/catalina]-
so that the logs look a bit cleaner, rather than tomcat-catalina-.log (which looks like it’s misconfigured).
This is all we need to do on the tomcat side, but I recommend making a backup of the file. This is because most upgrades will overwrite the $ARTIFACTORY_HOME/app folder, which will also overwrite the logging.properties file we added. I recommend checking out https://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/juli/FileHandler.html if you want to see more on your options here.
Step 2: Setting up logrotateThe second part is rotating the log. We can create a logrotate file like so
$ARTIFACTORY_HOME/var/log/tomcat/tomcat*.log {
size 25M
rotate 10000000000000
copytruncate
compress
missingok
olddir ../archived
dateext
extension .log
dateformat -%Y-%m-%d-%s
}
This logrotate file does the following:
- Only rotates the file if it’s over 25M
- Copies the log file to the $ARTIFACTORY_HOME/var/log/archived folder to be rotated, and clears out the current log file (basically what we expect with a log rotation)
- Compresses the file
- adds the date timestamp to the compressed log name
so it essentially behaves the same way as the current default Artifactory log rotation. The only difference is the
rotate 10000000000000
line, which essentially is the limit of rotated files. If you set this to something like 3, it will only keep 3 tomcat archived logs in the archived folder. Any new ones will cause the oldest archived log to be deleted. Setting it to an obscenely high number causes it to basically be unlimited.
There are other things you can do with logrotate, such as prerotate and postrotate scripts. In this case, we’ll leave it at that. Then you can add the logrotate operation to a cron job, something like
0 0 0 ? * MON * root /usr/sbin/logrotate /path/to/custom-logrotate.conf
which will run this every Monday at 00:00. Since the file is usually pretty small, we don’t really need a frequent CRON to rotate it.
And that’s it, now you have a tomcat-catalina.log and a tomcat-localhost.log (or whatever names you decided to call them) that will rotate the same way the default Artifactory logs do (25MB, into the log/archived folder) instead of creating tiny daily logs.