MongoDB is a cross-platform and document-oriented database program. It is classified as a NoSQL database, meaning it is not tabular and stores data differently than relational databases like MySQL. MongoDB uses JSON-like documents with optional schemas. This makes it easy to use and store data without having to worry about relationships and tables. And sometimes we would be looking at logs and wondering where are MongoDB logs stored.

Many large companies use MongoDB, and it’s easy to see why. MongoDB scales easily and is faster than an SQL database. If you are looking for a database that will not cost you time to think about relationships and scalability, then, MongoDB is a good choice. It is a leading open-source NoSQL database written in C++. It’s also free and has better performance. In this post, I’ll explain where you can look for MongoDB logs, how you can analyze them, and use the results of your analysis to solve problems. But before we step onto that let us see what exactly are MongoDB logs.

MongoDB logs

MongoDB logs contain information that, if used well, can save your business. It is helpful to know the problem before it becomes a problem. You can solve the problem before it even impacts the business. For example, if you use MongoDB as the database on your website, you can use the logs to fix an error before it affects users.

Where are MongoDB logs stored

Where are MongoDB logs stored?

By default, MongoDB creates the log file under this path:

/var/log/mongodb/mongodb.log

If the log file is not found, please check the MongoDB configuration file.

log path

You can thus check the MongoDB configuration file at:

 /etc/mongod.conf or /yourMongoDB-path/mongod.conf. 

# The log path defines where to log:
/etc/mongod.conf$ cat /etc/mongod.conf

Here’s how to use MongoDB with Python !! ~~> ~~>

/etc/mongod.conf$ cat /etc/mongod.conf
# mongod.conf # Where to store the data. 

# Note: if you run mongodb as a non-root user (recommended) you may manually need to create and set permissions for this directory.

dbpath=/var/lib/mongodb3#where to log
logpath=/var/log/mongodb/mongod.log#...

Now, let us see the several methods to find the path to MongoDB log files.

Finding the log file path using mongod.conf

By default, MongoDB creates the log file under the path:

"/var/log/mongodb/mongodb.log". 

# Note, if the log file is not found under this path, please check the MongoDB configuration file.

more /etc/mongod.conf

"systemLog" : 
{ 
     "destination" : "file", 
     "logAppend" : true, 
     "path" : "/var/log/mongodb/mongod.log" 
}

Using specialized command to find the MongoDB log file path

Another and the most easy method to find the path to the MongoDB log file is to use the getCmdLineOpts command. We can use it as follows:

db.getSiblingDB("admin").runCommand({getCmdLineOpts : 1})

"systemLog" : 
{ 
     "destination" : "file", 
     "logAppend" : true, 
     "path" : "/var/log/mongodb/mongod.log" 
}

So, by now you must know that the most important log file is mongod.log. You can specify the location of the log file when you start the mongod process. However, if you installed from a package on Ubuntu, the log file is usually located in:

/var/log/mongodb/mongod.log

# To retrieve the log file with the tail use:
tail -f /var/log/mongodb/mongod.log

# To display the log file in the MongoDB shell use:
show logs global

Use Grouping in MongoDB — Here’s how to !! ~~> ~~>

Now, let us see some other important concepts that we need to know.

Log Levels

MongoDB logs have a range of verbosity levels from 0 to 5. Thus, 0 is the quietest and 5 is the most verbose. Hence, 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 great attribute is that you can also specify the logging level for a particular component. So if you have a problem with the network, you can simply increase the level for that one component. Here’s how:

db.setLogLevel(verbosity from 0 to 5, [component])
db.setLogLevel(2,'Query')
Where are MongoDB logs stored

Want to know the MongoDB version that you have ? ~~> ~~>
What all components are there?

  • accessControl
  • command
  • control
  • geo
  • index
  • network
  • query
  • replication
  • storage
  • journal
  • writing

These were the components available for the log levels.

Note: Specifying the component is optional. If you do not specify a component, the new logging level is set for all components.

# To see the current logging levels, use:
db.getLogComponents()

# To reset the log level, use:
db.setLogLevel(-1,'Query')

# To set the logging level in mongod.conf, proceed as below:
storage: 
     dbPath: "/data"
systemLog:
      destination: file    
      path: "/var/log/mongodb/mongod.log"

component:
      query:
             verbosity: 2
      command:
              verbosity: 1

Note: MongoDB will use the new setting the next time Mongo is restarted.

Want to know when to shard in MongoDB? Click here to know !! ~~> ~~>

Query Profiling

You can set the profiling level to indicate slow queries. The format is:

db.setProfilingLevel(Level 0-2, threshold in ms)
db.setProfilingLevel(2,20)

# To view the details of a query, use:
db.system.profile.find({op: 'query', ns: 'mydatabase.mycollection'})

WRAPPING UP !! ~~> ~~>

In this post, we have seen what is log level, query profiling and where are MongoDB logs stored. We have also seen the various commands associated with this topic. Not only that, in addition to all these we also the various methods that we can get our hands on the log files in MongoDB. So, now just st back and relax. And, just enjoy this post and take back the learning as much as you can. On that note, until next time, see ya !! Goodbye !! ~~> ~~>

:: ~~> ~~> :: ** :::::: ** :: )) ** :: ** (( ~~> ~~>