How to log and trace NodeJS Events and Event handlers invocation?

青春壹個敷衍的年華 提交于 2019-12-05 15:59:05

https://github.com/joyent/node/blob/master/lib/events.js#L142-147

It emits a newListener event with the name and function.

Next up, rather than changing events.js, punch the prototype. Once you've required EventEmitter, you can monkeypatch it at runtime. This is bad practice generally, especially for something as critical as EventEmitter, but its fine for debugging your own program.

(function(){
  var old = EventEmitter.prototype.emit;
  EventEmitter.prototype.emit = ...
)();

Next up, log other handlers:

console.log(emitter.listeners('eventName'));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!