{: #git-wiki-toc}
This TypeScript library facilitates flexible and configurable event logging, aimed at making your event management seamless and effective.
At the heart of it is the event
function which can handle logging, event emitting, and error generation based on the options provided.
It integrates seamlessly with external tools such as EventEmitter2
.
You can easily control the actions performed by the function through a detailed configuration object, making the library highly flexible to adapt to various needs.
Below, you will find detailed insights into its core functionalities along with usage examples.
Also, it might be useful to read the Event Service that extends the event
functions and provides a more convenient way to integrate errors, event logging and emitting in your NestJS application.
event
function event<
TFormatter extends EventNameFormatter = EventNameFormatter,
TOption extends IEventOptions<TFormatter> = IEventOptions<TFormatter>,
>(
eventName: Parameters<TFormatter> | string,
options?: TOption,
): Promise<ReturnType<TOption>> | ReturnType<TOption>;
Here's what the core function offers:
Defaults:
Return Value: Depending upon the configurations set, it returns a promise or the return type defined in TOption.
import { event } from 'your-library-name';
import { EventEmitter2 } from 'eventemitter2';
event('USER_LOGIN', {
data: { userId: 12345 },
logger: {
level: 'log',
},
event: {
emitter: new EventEmitter2(),
},
});
Alongside the main event
function, we offer a roster of utility functions preset with different logging levels to make the logging process streamlined. These functions accept the same parameters as the event
function.
Each function corresponds to a predefined logging level in the options object, simplifying the setting of logging levels:
eventLogAsync
and eventLog
: LogLevelEnum.LOG
eventErrorAsync
and eventError
: LogLevelEnum.ERROR
eventWarnAsync
and eventWarn
: LogLevelEnum.WARN
eventDebugAsync
and eventDebug
: LogLevelEnum.DEBUG
eventVerboseAsync
and eventVerbose
: LogLevelEnum.VERBOSE
import { eventLog, eventError } from 'your-library-name';
eventLog('USER_CREATED', {
data: { userId: 12345 },
message: 'User created successfully',
});
throw eventError('USER_CREATION_FAILED', {
data: { userId: 12345 },
message: 'User creation failed',
});
Primarily internal, the applyAwaitOption
function allows you to set the await
option in the event options object to true by default.
applyAwaitOption
function applyAwaitOption<
TFormatter extends EventNameFormatter = EventNameFormatter,
>(options?: IEventOptions<TFormatter>): IEventOptions<TFormatter>;
This function takes an optional parameter and returns an IEventOptions object with the await
option set to true, providing a convenient way to apply asynchronous operations in your event handling logic.