#MongoDB 3.2.x Logging
The main log file is the mongod.log
. You can specify the log file location when you are starting the mongod process but if you have installed on Ubuntu from a package then you log file will normally be located in /var/log/mongodb/mongod.log
.
You can tail the log file using:
tail -f /var/log/mongodb/mongod.log
From the Mongo shell you can also view the log file using:
show logs
global
show log global
##Log Levels Mongo logs have a number of verbosity levels from 0 to 5. 0 is the quietest and 5 is the most verbose. The default level is 0.
The typical log entry follows the following pattern:
<timestamp> <severity> <components> [<context>] <message>
2016-03-03-07T35:22:33.456-700 I NETWORK [initandlisten] waiting...
One nice feature is that you canset the logging level for a specific component. So if you are having a problem wiht the network you can just increase the level for this one component. The command to do this is:
db.setLogLevel(verbosity from 0 to 5, [component])
db.setLogLevel(2,'query')
The components are:
- accessControl
- command
- control
- geo
- index
- network
- query
- replication
- storage
- journal
- write
Note: The component is optional but if you do not specify a component then the new logging level is set for all components.
To see the current log levels use:
db.getLogComponents()
You can reset the loglevel by setting it to -1 which means it will use the default level.
db.setLogLevel(-1,'query')
##Logging Configuration
You can set the logging level in the mongod.conf
.
storage:
dbPath: "/data"
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
component:
query:
verbosity: 2
command:
verbosity: 1
Mongo will use the new setting the next time mongo is restarted.
##Query Profiling You can set the Profiling Level so that you can flag slow queries. The format is:
db.setProfilingLevel(Level 0-2, Threshold in ms)
db.setProfilingLevel(2,20)
You can look at the details of a query using:
db.system.profile.find({op: 'query', ns: 'mydatabase.mycollection'})
##MongoStat
The mongostat
tool comes with mongo and it gives runtime statistics for mongod servers. To use it just attach it to a server using:
mongostat --host myserver --port 27017
If you do not specify a host or port it will assume the local server on the default port. You can also specify a specific number of rows ie --rowcount 10
, otherwise it will just keep producing statistics.
##MongoTop
The mongotop
tool show you where mongod spends monst of its time. Like mongostat it will continiously show data.
##db.stats()
You can run db.stats()
in the shell to get a picture of disk and memory usage.
db.stats()
{
"db" : "mydatabase",
"collections" : 19,
"objects" : 347914,
"avgObjSize" : 817.9144041343551,
"dataSize" : 284563872,
"storageSize" : 352567296,
"numExtents" : 72,
"indexes" : 28,
"indexSize" : 57101184,
"fileSize" : 520093696,
"nsSizeMB" : 16,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"dataFileVersion" : {
"major" : 4,
"minor" : 22
},
"ok" : 1
}
This returns the result in bytes. It is better to look at the data in megabytes (1000000) so we pass that to the db.stats() function.
db.stats(1000000)
{
"db" : "mydatabase",
"collections" : 19,
"objects" : 347914,
"avgObjSize" : 817.9144041343551,
"dataSize" : 284.563872,
"storageSize" : 352.567296,
"numExtents" : 72,
"indexes" : 28,
"indexSize" : 57.101184,
"fileSize" : 520.093696,
"nsSizeMB" : 16,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"dataFileVersion" : {
"major" : 4,
"minor" : 22
},
"ok" : 1
}
You can also just gets stats on a particular collection:
db.locations.stats(1000000)
{
"ns" : "mydatabase.locations",
"count" : 29123,
"size" : 55,
"avgObjSize" : 1896,
"numExtents" : 9,
"storageSize" : 58,
"lastExtentSize" : 20.64384,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 4,
"totalIndexSize" : 3,
"indexSizes" : {
"_id_" : 0,
"vendorName_Lower_1" : 0,
"geo_2dsphere" : 0,
"locationName_1" : 1
},
"ok" : 1
}
##Server Status
You can get the server runtime status by using db.serverStatus()
or running the db.runCommand({serverStatus:1})
command.
You can also just get a piece of information, for example the network status:
db.serverStatus().network
{
"bytesIn" : NumberLong(82899),
"bytesOut" : NumberLong(183990),
"numRequests" : NumberLong(402)
}
Or Memory:
db.serverStatus().mem
{
"bits" : 64,
"resident" : 76,
"virtual" : 660,
"supported" : true,
"mapped" : 0,
"mappedWithJournal" : 0
}
Or Cursors (which are a limited resource too)
db.serverStatus().metrics.cursor
{
"timedOut" : NumberLong(0),
"open" : {
"noTimeout" : NumberLong(0),
"pinned" : NumberLong(2),
"total" : NumberLong(2)
}
}