Optionaloptions: LoggerOptionsConfiguration options for the logger
Optional[captureThe Symbol.for('nodejs.rejection') method is called in case a
promise rejection happens when emitting an event and
captureRejections is enabled on the emitter.
It is possible to use events.captureRejectionSymbol in
place of Symbol.for('nodejs.rejection').
import { EventEmitter, captureRejectionSymbol } from 'node:events';
class MyClass extends EventEmitter {
constructor() {
super({ captureRejections: true });
}
[captureRejectionSymbol](err, event, ...args) {
console.log('rejection happened for', event, 'with', err, ...args);
this.destroy(err);
}
destroy(err) {
// Tear the resource down here.
}
}
Registers an event listener for the specified event (alias for on).
The type of event to listen for.
This instance for chaining.
Closes the logger, flushes pending logs, and releases resources.
Logs a message at the DEBUG level to the specified sinks.
The log message object
Optional array of sink names to log to; logs to all sinks if omitted
Emits an event with the specified payload.
The type of event to emit.
Whether the event had listeners.
Logs a message at the ERROR level to the specified sinks.
The log message object
Optional array of sink names to log to; logs to all sinks if omitted
Returns an array listing the events for which the emitter has registered listeners.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});
const sym = Symbol('symbol');
myEE.on(sym, () => {});
console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
Flushes all pending logs and waits for them to be processed.
Logs a message at the INFO level to the specified sinks.
The log message object
Optional array of sink names to log to; logs to all sinks if omitted
Returns the number of listeners for the specified event.
The type of event.
The event name.
The number of listeners.
Logs a message at the TRACE level to the specified sinks.
The log message object
Optional array of sink names to log to; logs to all sinks if omitted
Removes an event listener for the specified event (alias for removeListener).
The type of event.
This instance for chaining.
Registers a one-time event listener for the specified event.
The type of event to listen for.
This instance for chaining.
Adds a listener to the beginning of the listeners array for the specified event.
The type of event.
This instance for chaining.
Adds a one-time listener to the beginning of the listeners array for the specified event.
The type of event.
This instance for chaining.
Registers a new sink (logging destination) with the logger.
The logger instance with new type (for chaining)
Removes all listeners, or those of the specified eventName.
It is bad practice to remove listeners added elsewhere in the code,
particularly when the EventEmitter instance was created by some other
component or module (e.g. sockets or file streams).
Returns a reference to the EventEmitter, so that calls can be chained.
OptionaleventName: string | symbolBy default EventEmitters will print a warning if more than 10 listeners are
added for a particular event. This is a useful default that helps finding
memory leaks. The emitter.setMaxListeners() method allows the limit to be
modified for this specific EventEmitter instance. The value can be set to
Infinity (or 0) to indicate an unlimited number of listeners.
Returns a reference to the EventEmitter, so that calls can be chained.
Logs a message at the WARN level to the specified sinks.
The log message object
Optional array of sink names to log to; logs to all sinks if omitted
Creates a new Logger instance with the specified options.