Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js

Logging

On this page

  • Overview
  • Log Levels
  • Filter on a Specific Class
  • Custom Logger

This guide shows you how to configure the driver logger which is responsible for reporting events that occur while your application is running. You can set the logger level, specify logging settings for individual classes, and provide a custom logger implementation.

You should read this guide if you need to customize the default behavior of the driver logger or want to explore the information provided by the logger.

You can configure the driver to report logger messages at three different levels as shown in the following table.

Logger Level
Description
debug
The most verbose logger level which shows all logger messages reported by the driver including those reported in the info and error levels.
info
Include informational messages reported by the driver including those reported in the error level.
error
Only include error messages reported by the driver. This is the default setting.

The following example demonstrates how to set the logger to the debug level:

const { MongoClient, Logger } = require("mongodb");
// Replace the following with your MongoDB deployment's connection string.
const uri =
"mongodb+srv://<clusterUrl>/?replicaSet=rs&writeConcern=majority";
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function main(client) {
// Set debug level
Logger.setLevel("debug");
const db = client.db("sample_mflix");
// Run a sample command to produce logger messages
await db.command({ hello: 1 });
}

This snippet should output a few lines of logging information that resemble the following:

[DEBUG-Db:63846] 1585699200000 executing command {"hello":1} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}] {
type: 'debug',
message: 'executing command {"hello":1} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}]',
className: 'Db',
...,
}
[DEBUG-Server:63846] 1585699200000 executing command [{"ns":"sample_mflix.$cmd","cmd":{"hello":1},"options":{}}] against myClusterHostname:27017:27017 {
...
}
[DEBUG-Server:63846] 11585699200000 executing command
[{"ns":"admin.$cmd","cmd":{"endSessions":[{"id":"DQLUQ1/LRKOYMy2CD13iLQ=="}]},"options":{}}] against myClusterHostname:27017 {
...
}

You can set the Logger to only produce log messages generated by specific classes by defining a filter on it. The following example demonstrates how to apply a filter to log messages from the Db class only.

// Set debug level
Logger.setLevel("debug");
// Only log statements on "Db" class
Logger.filter("class", ["Db"]);
const db = client.db("sample_mflix");
// Execute command { hello: 1 } against db
await db.command({ hello: 1 });

The logger output of the code above is similar to the prior example, but excludes logging from classes other than Db such as Server and resembles the following:

[DEBUG-Db:63846] 1585699200000 executing command {"hello":1} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}] {
type: 'debug',
message: 'executing command {"hello":1} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}]',
className: 'Db',
...

You can specify any number of the following driver classes in the logging filter array to control what information is passed to the Logger:

Class name
Description
Db
Information related to calls on a db instance
Server
Information related to a server instance (standalone instance, mongos instance, or replica set member).
ReplSet
Information related to a replica set.
Mongos
Information related to a mongos instance.
Cursor
Information related to a cursor.
Pool
Information related to a connection pool.
Connection
Information related to a single-instance connection.
Ping
Information related to a replica set ping operation.

You can add your own classes to the logger by creating your own logger instances as shown in the following sample code.

const { Logger } = require("mongodb");
class A {
constructor() {
this.logger = new Logger("A");
}
doSomething() {
if (this.logger.isInfo()) {
this.logger.info("logging A", {});
}
}
}
// Execute A to produce logging messages
const a = new A();
a.doSomething();

The logging message is triggered when doSomething() is called and is presented in the following format:

[INFO-A:65156] 1585699200000 logging A {
type: 'info',
message: 'logging A',
className: 'A',
...
}

You can configure the behavior and format of the messages reported by the logger by passing in a logger callback function which allows you to access and modify the message and metadata of the logger message.

The following example demonstrates how to define a logger callback function and add custom logic to it.

// Set debug level
Logger.setLevel("debug");
// Set our own logger
Logger.setCurrentLogger((msg, context) => {
// Add your custom logic here
context['foo'] = 'bar';
msg = "Hello, World! " + msg;
console.log(msg, context);
});
const db = client.db("sample_mflix");
// Run a command to produce logger messages
await db.command({ hello: 1 });

The code example above adds additional information in the callback function. The output of the example resembles the following:

Hello, World! [DEBUG-Db:65873] 1585699200000 executing command {"hello":1} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}] {
type: 'debug',
message: 'executing command {"hello":1} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}]',
className: 'Db',
foo: 'bar',
...
}

For more information on the methods available on the Logger, see the Logger API documentation.

←  CollationsMonitoring →