MongoDB only rotates logs in response to the logRotate command, or when the mongod
or mongos
process receives a SIGUSR1
signal from the operating system.
Example 1: manually
# send SIGUSR1 signal sudo kill -SIGUSR1 $(cat /var/lib/mongodb/mongod.lock) # delete old logs sudo rm -rf /var/log/mongodb/mongod.log.*
Example 2: logrotate command
logrotate
is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.
Copy the following code into /etc/logrotate.d/mongodb
and make sure that pathes and filenames in the script correspond with those in your system.
/var/log/mongodb/mongodb.log { daily rotate 7 compress missingok notifempty sharedscripts postrotate /bin/kill -SIGUSR1 `cat /var/lib/mongodb/mongod.lock` && rm -f /var/log/mongodb/mongodb.log.????-??-??T??-??-?? endscript }
daily
- log files are rotated every dayrotate count
- log files are rotated count times before being removed or mailedcompress
- old versions of log files are compressed with gzip by default.missingok
- if the log file is missing, go on to the next one without issuing an error message. notifempty
- do not rotate the log if it is empty. sharedscripts
- normally, prescript
and postscript
scripts are run for each log which is rotated, meaning that a single script may be run multiple times for log file entries which match multiple files. If sharedscript
is specified, the scripts are only run once, no matter how many logs match the wildcarded pattern. postrotate/endscript
- the lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated.