0
votes

i'm trying to use my custom logger service inside other services, but i have noticed that when i'm provide logger service to other service didn't call any lifecycle hooks anymore

there is code of my logger service

@Injectable({ scope: Scope.REQUEST })
export class LoggerService {
    constructor(
        @Inject(REQUEST) private readonly payload: RequestContextHost<RequestPayloadWithCorrelationId, TcpContext>
    ) {}
  .... // other methods
}

I need this class for get unique request id and log it, but i use this service in another which implements OnModuleInit method, so when i provide logger to this service, it just don't call onModuleInit method

Maybe getting request id for logger is wrong?

1

1 Answers

0
votes

Lifecycle hooks are not called for request scoped dependencies as per the docs

The lifecycle hooks listed above are not triggered for request-scoped classes. Request-scoped classes are not tied to the application lifecycle and their lifespan is unpredictable. They are exclusively created for each request and automatically garbage-collected after the response is sent.

It seems like you'll need another method around using a request scoped logger, at least for this class.