@nodiator/extension-logger
v4.0.2
Published
Logger extension for Nodiator
Downloads
50
Readme
Logger extension for Nodiator
Provides low configuration logging and visibility utility for tracking messages execution.
Table of contents
Installation
npm i @nodiator/extension-logger
# or
yarn add @nodiator/extension-logger
Quick Start
import { LoggerExtension } from '@nodiator/extension-logger';
const mediator = MediatorFactory.create();
mediator.use(new LoggerExtension());
Example output
[Mediator] Requested GetHistoryUseCase (id=14e8899b-85e4-4936-9852-3933f71dd4dc)
[Mediator] -- handling GetHistoryUseCase (id=14e8899b-85e4-4936-9852-3933f71dd4dc) with HistoryPipeline
[Mediator] -- handling GetHistoryUseCase (id=14e8899b-85e4-4936-9852-3933f71dd4dc) with GetHistoryUseCaseHandler
[Mediator] -- GetHistoryUseCaseHandler responded to GetHistoryUseCase (id=14e8899b-85e4-4936-9852-3933f71dd4dc)
[Mediator] -- HistoryPipeline responded to GetHistoryUseCase (id=14e8899b-85e4-4936-9852-3933f71dd4dc)
[Mediator] -- GetHistoryUseCase (id=14e8899b-85e4-4936-9852-3933f71dd4dc) took 0.009s
[Mediator] GetHistoryUseCase (id=14e8899b-85e4-4936-9852-3933f71dd4dc) handled
Based on express example.
Levels
Extension supports following levels of logging: DEBUG
, INFO
, WARN
, ERROR
and NONE
. The default one is INFO
and allows all logs to be visible besides those marked as DEBUG
.
mediator.use(
new LoggerExtension({
dynamicOptions: () => ({ level: MediatorLoggingLevels.DEBUG }),
})
);
Note that dynamicOptions
property is function called each time configuration data is needed which allows to implement logic to change logging behaviour without app reload.
Custom logger
By default messages are logged via console
methods. This behavior can be overwritten by
class CustomLogger implements MediatorLogger {
debug(msg: string) {
...
}
info(msg: string) {
...
}
warn(msg: string) {
...
}
error(msg: string) {
...
}
}
mediator.use(
new LoggerExtension({
logger: new CustomLogger(),
})
);
Exceptions levels override
Some exceptions may not necessarily fit into error level. This behavior can be customized via
const mediator = MediatorFactory.create({
exceptionsLoggingLevels: {
[MediatorLoggingLevels.WARN]: [MessageTimeoutException],
},
});
With above configuration all mediator timeout exceptions will be logged as warnings.
License
This project is licensed under the MIT License - see the LICENSE file for details.