how to enable debug on node.js and mongoDB native driver?
Asked Answered
C

3

6

i use node.js and node-mongodb-native driver, with connection pooling. is there any way to enable debug for see what's happening, how many connections are active and when a connection is opened or closed?

i would like to see something like:

* connection xxx opened on host:port
* connection yyy opened on host:port
* connection xxx closed
Circular answered 2/10, 2012 at 14:31 Comment(0)
A
1

The Db() and Server() objects both support a logger option, which is an object with log, error and debug functions. The Db() option doesn't appear to be documented at the moment, but it is mentioned in the 0.9.6-20 2011-10-04 changelog entry.

I'm not sure if all of the information you need is supported with this interface, but it's definitely a good place to start. The driver team would also probably welcome a pull request to add such features.

Apiculture answered 2/10, 2012 at 15:14 Comment(3)
thank you, i modified connection.js to log connections and pulled a request on githubCircular
How did you get this working? I passed on an object supporting those methods that should log to the console but I'm not seeing didly squat.Burgener
If it helps, the pull request was: github.com/mongodb/node-mongodb-native/pull/765Apiculture
S
4
  1. To watch the commands been sent to MongoDB, set the driver logger's level to debug.
  2. To react to connection pool events, just subscribe to them and log yourself.
  3. You may need the topology monitoring to react to changes of topology, such as joins to a secondary or disconnections with a replica set.
const client = new MongoClient('mongodb://127.0.0.1:27017/', {
  useUnifiedTopology: true,
  loggerLevel: 'debug',
  // logger: (message, context) => console.dir(context),
})

// connection pool monitoring
client.on('connectionPoolCreated', event => console.dir(event))
client.on('connectionPoolClosed', event => console.dir(event))
client.on('connectionCreated', event => console.dir(event))
client.on('connectionReady', event => console.dir(event))
client.on('connectionClosed', event => console.dir(event))
client.on('connectionCheckOutStarted', event => console.dir(event))
client.on('connectionCheckOutFailed', event => console.dir(event))
client.on('connectionCheckedOut', event => console.dir(event))
client.on('connectionCheckedIn', event => console.dir(event))
client.on('connectionPoolCleared', event => console.dir(event))

// topology monitoring
client.on('serverDescriptionChanged', event => console.dir(event))
client.on('serverHeartbeatStarted', event => console.dir(event))
client.on('serverHeartbeatSucceeded', event => console.dir(event))
client.on('serverHeartbeatFailed', event => console.dir(event))
client.on('serverOpening', event => console.dir(event))
client.on('serverClosed', event => console.dir(event))
client.on('topologyOpening', event => console.dir(event))
client.on('topologyClosed', event => console.dir(event))
client.on('topologyDescriptionChanged', event => console.dir(event))
Soupandfish answered 26/4, 2020 at 17:13 Comment(0)
A
1

The Db() and Server() objects both support a logger option, which is an object with log, error and debug functions. The Db() option doesn't appear to be documented at the moment, but it is mentioned in the 0.9.6-20 2011-10-04 changelog entry.

I'm not sure if all of the information you need is supported with this interface, but it's definitely a good place to start. The driver team would also probably welcome a pull request to add such features.

Apiculture answered 2/10, 2012 at 15:14 Comment(3)
thank you, i modified connection.js to log connections and pulled a request on githubCircular
How did you get this working? I passed on an object supporting those methods that should log to the console but I'm not seeing didly squat.Burgener
If it helps, the pull request was: github.com/mongodb/node-mongodb-native/pull/765Apiculture
P
1

You can use the node's driver Logger class:

import { Logger } from "mongodriver";

And later in your code:

Logger.setLevel("debug");

You can check documentation on the official driver API doc

Phooey answered 1/4, 2020 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.