- To watch the commands been sent to MongoDB, set the driver logger's level to
debug
.
- To react to connection pool events, just subscribe to them and log yourself.
- 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))