nestlogger
nestlogger copied to clipboard
setContext does not work correct
export class SecurityService {
constructor(
private readonly logger: LoggerService
) {
this.logger.setContext(`SecurityService`);
}
}
export class SecurityService1 {
constructor(
private readonly logger: LoggerService
) {
this.logger.setContext(`SecurityService1`);
}
}
it alway get context is SecurityService
LoggerService is a singleton when injected to another services so it can have only one context. It would be nice if you could use it like this though.
You could probably inject it in request scope to make it work but there's a performance cost if done like that. Here's the doc how to do it: https://docs.nestjs.com/fundamentals/injection-scopes
So i mean if i want to have multiple context. I must be create many logger instance. And it effect to performent of my application
It means that a new instance is created on each request. That shouldn't be too bad but on a heavy load it would affect. It would be possible to create multiple instances as well but I'm not sure how that would affect if they're configured to use the same log file. The easiest and most robust thing would be to set the context on every log call.
That being said, it won't work too good currently. However, there might be some ways to fix this. I can take a look at it at some point. Or if you have any ideas, I'm more than happy to accept a pull request for this.
thanks, i think i will setContext every log call.