简体   繁体   English

如何在 node.js 和 mongoDB 本机驱动程序上启用调试?

[英]how to enable debug on node.js and mongoDB native driver?

i use node.js and node-mongodb-native driver, with connection pooling.我使用 node.js 和 node-mongodb-native 驱动程序,以及连接池。 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
  1. To watch the commands been sent to MongoDB, set the driver logger's level to debug .要查看发送到 MongoDB 的命令,请将驱动程序记录器的级别设置为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))

The Db() and Server() objects both support a logger option, which is an object with log , error and debug functions. Db()Server()对象都支持 logger 选项,这是一个具有logerrordebug功能的对象。 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 .目前似乎没有记录Db()选项,但在0.9.6-20 2011-10-04 更改日志条目中提到了它。

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.驱动程序团队也可能会欢迎添加此类功能的拉取请求。

You can use the node's driver Logger class:您可以使用节点的驱动程序Logger类:

import { Logger } from "mongodriver";

And later in your code:稍后在您的代码中:

Logger.setLevel("debug");

You can check documentation on the official driver API doc您可以查看官方驱动程序 API文档上的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM