Note: You are currently viewing version 2.2 of the Node.js driver documentation.
Click here for the latest version.
- Reference
- Management
- Logging
Logging
You can change the log level, filter on classes to allow only specific classes to log, and provide your own logger implementation.
Setting Log level
The driver allows logging at three different levels: debug
,
info
and error
. The default level is error
.
The following example demonstrates how to set the logger to debug
.
var MongoClient = require('mongodb').MongoClient
, Logger = require('mongodb').Logger
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
// Set debug level
Logger.setLevel('debug');
// Execute command {ismaster:true} against db
db.command({ismaster:true}, function(err, d) {
assert.equal(null, err);
db.close();
});
});
Filtering On specific classes
You can set the Logger to only log specific class names. The following example
demonstrates how to log only the Db
class.
var MongoClient = require('mongodb').MongoClient
, Logger = require('mongodb').Logger
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
// Set debug level
Logger.setLevel('debug');
// Only log statements on 'Db' class
Logger.filter('class', ['Db']);
// Execute command {ismaster:true} against db
db.command({ismaster:true}, function(err, d) {
assert.equal(null, err);
db.close();
});
});
Driver classes available for filtering:
Db
: The Db instance log statementsServer
: A server instance (either standalone, a mongos or replica set member)ReplSet
: Replica set related log statementsMongos
: Mongos related log statementsCursor
: Cursor log statementsPool
: Connection Pool specific log statementsConnection
: Singular connection specific log statementsPing
: Replica set ping inquiry log statements
You can add your own classes to the logger by creating your own logger instances.
var Logger = require('mongodb').Logger
, assert = require('assert');
var A = function() {
var logger = Logger('A', options);
this.do = function() {
if(logger.isInfo()) logger.info('logging A', {});
}
}
// Execute A
var a = new A();
a.do();
Custom logger
The following example demonstrates how to define a custom logger.
var MongoClient = require('mongodb').MongoClient
, Logger = require('mongodb').Logger
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
// Set debug level
Logger.setLevel('debug');
// Set our own logger
Logger.setCurrentLogger(function(msg, context) {
console.log(msg, context);
});
// Execute command {ismaster:true} against db
db.command({ismaster:true}, function(err, d) {
assert.equal(null, err);
db.close();
});
});
On this page