I'm trying to inject nestwinston globally in my nestjs app and also to keep a beautiful format in my console/terminal and in Grafana, and most important to display custom objects where needed, but using a beautiful format, not simply JSON.
My problem is that the nestwinston changes its behavior when is injected globally.
I followed https://github.com/gremo/nest-winston#replacing-the-nest-logger-also-for-bootstrapping, but when I'm running the next code sample, my error log's object is not displayed in log whatever format type I use (tried all of them and the only option was to format the log as simple JSON, which looks awful in Grafana):
import { Injectable, Logger } from '@nestjs/common';
...
constructor(
private readonly logger: Logger,
) {
this.logger.log("my message", {myValue: true});
}
result:
[NestWinston] Info [Bootstrapper] my message - {}
see that {myValue: true} is not displayed in log... even it displays an empty object which I really don't know what it is...
I achived that by creating a custom
LoggerServicein which I use thenestwinstonalongside with a custom logger formatter, then replacing the nestjs default logger with this service, so it will be used everywhere in my project.LoggerService:main.ts:then in any module you want to use the new Logger, add it to providers:
and then to any module's service:
=>