How to register and track calls of NodeJS event and event handlers?

Is there a way to register all other registered event handlers when the event handler is registered?

Is there also a way to log all the events emitted and the name of the handler functions that fire when an event occurs at run time?

If a nodejs application fires chain events (one event fires another), and each event has multiple handlers, when an exception occurs in the sheet of the chain of event handlers, stacktrace does not show full context information.

An event log and information for handlers would be very useful in such a situation.

One hackey solution is to add (conditionally) to the https://github.com/joyent/node/blob/master/lib/events.js log, but I'm sure there should be a better way.

+4
source share
1 answer

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

It throws a newListener event with a name and a function.

Next, instead of changing events.js, break through the prototype. After you need an EventEmitter, you can disable it at run time. This is bad practice in general, especially for something as important as EventEmitter, but it is great for debugging your own program.

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

Next, write down the other handlers:

 console.log(emitter.listeners('eventName')); 
+3
source

Source: https://habr.com/ru/post/1436974/


All Articles