Configuration MongoDB logs rotation
Log files are critical for analyzing errors encountered when executing platform processes. However, outdated logs can take up a significant amount of memory on the server. In order for the system to clean them regularly, it is necessary to configure log rotation. In this article we will describe log rotation for MongoDB.
To configure rotation using the logrotate utility, create the /etc/logrotate.d/mongod file with the following contents:
/var/log/mongodb/*.log {
size 100M
rotate 7
compress
missingok
notifempty
sharedscripts
copytruncate
postrotate
/bin/kill -SIGUSR1 $(cat /var/lib/mongodb/mongod.lock 2> /dev/null) 2> /dev/null || true
endscript
}
When installing MongoDB from a deb package, the above file is created automatically. You can edit it to customize the rotation to your liking.
The main parameters of the script:
size- the size above which the file will be rotated at the nextlogrotatestartuprotate- number of saved file rotationscompress- old versions of log files will be compressed withgzipnotifempty- do not save the log file in rotation if it is empty
Above is the recommended configuration of the utility, you can customize it as you wish. All possible parameters are listed on this page.