Logger Enhancements
Is there an existing issue that is already proposing this?
- [X] I have searched the existing issues
Is your feature request related to a problem? Please describe it
There is no way to provide a custom logger that has injectable dependencies since you can't provide those providers (other modules, etc) to Terminus when calling TerminusModule.forRoot()
Terminus is just taking the class-type and passing it as a useClass provider.
Describe the solution you'd like
It would be useful if the terminus module could default to using the Nest provided logger, which can be overridden when doing app.useLogger. This would allow for custom loggers an application is using to just be re-used by Terminus without any additional configuration.
Alternatively, the options to TerminusModule.forRoot could be enhanced to be able to provide the dependencies needed to create an instance of the custom logger.
Teachability, documentation, adoption, migration strategy
If the first suggestion is adopted, users wouldn't have to change anything about how they declare TerminusModule.
If a different approach is chosen, maybe it could look like
TerminusModule.forRootAsync({
imports: [SomeDependentModule],
inject: [...dependencies],
useFactory: (...dependencies) => ({
logger: new CustomLogger(...dependencies),
...otherTerminusOptions
})
})
What is the motivation / use case for changing the behavior?
I have a custom LoggerModule that exports a LoggerService that re-uses a single Pino logger instance. This LoggerService's dependencies, in addition to the pino logger, also has some stuff in it for AsyncContext storage management to enhance logs, but the main priority is that the entire log needs to be in JSON format to be ingested by Humio in the format expected for structured logging, which my custom logger handles.
Currently Terminus's errorLogStyle doesn't change the entire log to JSON but only the details
[Nest] 64381 - 05/01/2024, 11:48:10 AM ERROR [HealthCheckService] Health Check has failed! {"rabbitmq":{"status":"up","uri":"amqp://localhost:5672"},"postgres":{"status":"down"}}
but that should really look like
{
"context": "HealthCheckService",
"@timestamp": "2024-01-05T11:48:10.000Z",
"level": "ERROR",
"message": "Health Check has failed!",
"details": {
"rabbitmq": { "status": "up", "uri": "amqp://localhost:5672" },
"postgres": { "status": "down" }
}
}
in my case.
Sorry for the late response. I've implemented a version where we'd use the default logger, hence you can overwrite it with app.useLogger(...). Would you like to verify my implementation by installing the latest beta? npm -i --save @nestjs/terminus@beta?
Hi @BrunnerLivio, I installed the beta version "10.3.0-beta.0" in my project and tested if it uses the global defined Logger (LoggerService). It works as expected 👍 .
This has been release with v10.3.0.