Logging

MongoDB outputs some important information to stdout while its running. There are a number of things you can do to control this

Command Line Options

  • --quiet - less verbose output (more details)
  • -v - more verbose output. use more v's (such as -vvvvvv) for higher levels of verbosity. To change the logging verbosity on a running instance, you can use the setParameter Command
  • --logpath <file> - output to file instead of stdout
    • If you use logpath, you can rotate the logs by either running the logRotate command (1.3.4+) or sending SIGUSR1
    • You should always use --logappend with --logpath to append to the existing log file, instead of overwriting it

Rotating the log files

Log file rotation renames the current log file to the same name with a timestamp file extension appended, then continues logging to a new file with the original name. The timestamp is the time that the logRotate command was executed, expressed in UTC (GMT) and formatted as ISO but with dashes instead of colons for the time portion.

For example:

$ ./mongod -v --logpath /var/log/mongodb/server1.log --logappend

will start mongod with verbose logging to /var/log/mongodb/server1.log, appending to any existing log file.

In another terminal, list the matching files:

$ ls /var/log/mongodb/server1.log*
server1.log

Rotate the log file using one of the methods described below, then list the files again:

$ ls /var/log/mongodb/server1.log*
server1.log  server1.log.2011-11-24T23-30-00

This indicates a log rotation performed at exactly 11:30 pm on November 24th, 2011 UTC, which will be the local time offset by the local time zone. The original log file is the one with the timestamp, and new log lines are now being written to the server1.log file.

If another logRotate command is given one hour later, an additional file will appear:

$ ls /var/log/mongodb/server1.log*
server1.log  server1.log.2011-11-24T23-30-00  server1.log.2011-11-25T00-30-00

The server1.log.2011-11-24T23-30-00 file is unchanged from before, while server1.log.2011-11-25T00-30-00 is the previous server1.log file renamed and server1.log is a new empty file that will receive new log output.

From the mongo shell

> use admin
> db.runCommand("logRotate");
Windows
The logRotate command is available on Windows in version 2.1.0 and higher

From the unix shell

Rotate logs for a single process

shell> kill -SIGUSR1 <mongod process id>

Rotate logs for all mongo processes on a machine

shell> killall -SIGUSR1 mongod
Windows
Windows does not have an equivalent to the unix kill -SIGUSR1 feature, but the mongo shell can be used from the Windows command line using a JavaScript command file to issue a logRotate command.
C:\> type logRotate.js
db.getMongo().getDB("admin").runCommand("logRotate")
C:\> mongo logRotate.js
C:\> rem Log files rotated, still at Windows command prompt

Accessing Logs via Shell

New in 1.9.x
See the getLog Command for more details

Get a list of available loggers

> show logs
> db.runCommand( { getLog : "*" } )

Get a log

> show log global
> db.runCommand( { getLog : "global" } )

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE FORUMS: http://groups.google.com/group/mongodb-user. Post tips and clarifications here.

blog comments powered by Disqus