nestjs / terminus

Terminus module for Nest framework (node.js) :robot:
https://nestjs.com/
MIT License
678 stars 99 forks source link

Logger Enhancements #2547

Open ssilve1989 opened 6 months ago

ssilve1989 commented 6 months ago

Is there an existing issue that is already proposing this?

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.

BrunnerLivio commented 4 months ago

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?