Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB log file growth

Tags:

mongodb

Currently my log file sits at 32 meg. Did I miss an option that would split the log file as it grows?

like image 984
strickland Avatar asked Feb 15 '11 14:02

strickland


People also ask

Can we delete MongoDB log file?

The advantage of commands is that log rotation can be enforced without restarting MongoDB. User can then delete old log files if necessary.

How do I view MongoDB logs?

MongoDB logs can be found in the MongoDB log files at /var/log/mongodb/mongodb. log. If you can't find the log files from this location, you can check the mongodb.

What is MongoDB log?

As part of normal operation, MongoDB maintains a running log of events, including entries such as incoming connections, commands run, and issues encountered. Generally, log messages are useful for diagnosing issues, monitoring your deployment, and tuning performance.


3 Answers

You can use logrotate to do this job for you.

Put this in /etc/logrotate.d/mongod (assuming you use Linux and have logrotate installed):

/var/log/mongo/*.log {
    daily
    rotate 30
    compress
    dateext
    missingok
    notifempty
    sharedscripts
    copytruncate
    postrotate
        /bin/kill -SIGUSR1 `cat /var/lib/mongo/mongod.lock 2> /dev/null` 2> /dev/null || true
    endscript
}
like image 63
pdwalker Avatar answered Oct 05 '22 10:10

pdwalker


If you think that 32 megs is too large for a log file, you may also want to look inside to what it contains.

If the logs seem mostly harmless ("open connection", "close connection"), then you may want to start mongod with the --quiet switch. This will reduce some of the more verbose logging.

like image 31
Gates VP Avatar answered Oct 05 '22 10:10

Gates VP


Using logrotate is a good option. while, it will generate 2 log files that fmchan commented, and you will have to follow Brett's suggestion to "add a line to your postrotate script to delete all mongod style rotated logs".

Also copytruncate is not the best option. There is always a window between copy and truncate. Some mongod logs may get lost. Could check logrotate man page or refer to this copytruncate discussion.

Just provide one more option. You could write a script that sends the rotate signal to mongod and remove the old log files. mongologrotate.sh is a simple reference script that I have written. You could write a simple cron job or script to call it periodically like every 30 minutes.

like image 26
CloudStax Avatar answered Oct 05 '22 09:10

CloudStax