#ExecutionContext in Logger

4 messages · Page 1 of 1 (latest)

granite epoch
#

Hi, I want to extend the built-in logger like here:

https://docs.nestjs.com/techniques/logger

However, my hope is to be able to include a user identifier in the logs.

Is there a way to get the Request object or context similar to:

const request = context.switchToHttp().getRequest<Request>();

If I could do that inside the logger, that'd be fantastic.

zenith void
#

You can make your logger REQUEST scoped, which I don't really suggest, but it is an option, or you can use a tool like nestjs-cls to make use of AsyncLocalStorage to help provide REQUEST scoped-like properties without actually being request scoped

granite epoch
#

Yeah, I think making the logger request scoped would be the wrong path 😂

#

Let me look into nestjs-cls