[FEATURE REQUEST] Provide context per class
See original GitHub issueIs your feature request related to a problem? Please describe.
I’m using globally imported pino logger module. Every time when I make a log line I would like to know from which class the log was created. Right now, I have to include the context manually. Like the following lines in the README.md
:
https://github.com/iamolegga/nestjs-pino/blob/master/README.md#L56
https://github.com/iamolegga/nestjs-pino/blob/master/README.md#L72
Currently, we can set base
context per configuration of logger module. That’s too broad for my use case. Even if I migrate my logger module import from global to per module import, I’d prefer finer level of granularity in defining base
context. (But not as fine as log line 😃 )
Describe the solution you’d like
I would like to write log lines using injected logger without manually providing class context on each log line. I’d like the context to be either set up once per class (injection) or somehow dynamically configured to use class name (or whatever else) per module configuration.
I’m probably off on these, but my superficial suggestions would be:
- configure the module with an option to always fork a child logger for each class it’s injected into OR
- expose
child
functionality so we can fork (child) a logger manually. Then we could set it up with context in each class constructor (or wherever we deem useful)
Issue Analytics
- State:
- Created 4 years ago
- Comments:17 (10 by maintainers)
Top GitHub Comments
I would agree with @trymoto because I would prefer to KISS, and actually get rid of all not necessary extra API, and make library users not to remember complex API with this
@PinoLogger.Inject
orInjectLogger
.I think that injecting it as regular service is more preferred, and makes setting context optional, so
PinoLogger
users may use it without required context setting, and use it just likepino
and not thinking aboutcontext
property, that actually came up fromLoggerService
🤔Hey, I’ve almost done with it, but just need add tests and docs, so I guess it will be shipped in several hours. The api of new
PinoLogger
will be like:And I’m still in doubt, which varian of injection and setting context is better:
@PinoLogger.Inject
like above or@maricn @valerio-battaglia what do you think?