I wanted to do log rotation of MongoDB, so I checked and set it. (Version is 3.4)
I want to rotate the log once a day. I want to name the log file after rotation mongod.log_yyyymmdd. I want to compress automatically. I want to automatically delete it after a certain period of time instead of continuing to accumulate it.
Set as follows in logrotate
# vi /etc/logrotate.d/mongod
/usr/local/var/log/mongodb/mongod.log
{
missingok
daily
dateext
rotate 30
create
compress
delaycompress
postrotate
/bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod.pid 2> /dev/null` 2> /dev/null || true
find /data/mongodb/logs -type f -size 0 -regextype posix-awk -regex "^\/usr\/local\/var\/log\/mongodb\/mongod\.log\.[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}-[0-9]{2}-[0-9]{2}$" -execdir rm {} \; >/dev/null 2>&1
endscript
}
When I log rotate MongoDB with kill -SIGUSR1, MongoDB creates an empty log file named mongod.log.yyyy-mm-ddTxx-xx-xx, so I use find to delete it.
If you choose nocreate instead of create, MongoDB will lose track of the log files MongoDB terminates abnormally at the timing of rotation.