I have a nestjs-pino logger set up within my NestJS API as prescribed by the official docs.
@Module({
imports: [
LoggerModule.forRoot({
pinoHttp: {
customProps: (req) => ({
reqId: req.headers['my-tracking-header'] ?? uuidv4() // uuid lib
})
}
}),
MyModule
],
controllers: [AppController],
})
I am using the logger within my Service files via dependency injection as expected, where I am calling util functions held within separate non class-based util files in which I am wanting to log out from.
MyModule.module.ts
@Module({
controllers: [MyController],
providers: [MyService],
})
export class MyModule {}
MyService.module.ts
@Injectable()
export class ContextService {
constructor(private readonly logger: Logger) {}
serviceFunction(): any {
this.logger.log('logging from the service');
myUtil(this.logger, 'foo'); // Passing the logger as a argument
}
}
The only quick and dirty solution I’ve had is to actually pass the logger as an argument to my util functions, but this definitely doesn’t feel like the optimal solution.
MyUtils.util.ts
// Taking the logger as an arg
export const myUtil(logger: Logger, content: string): any {
logger.log({content}, "Logging from the util");
}
Is there a correct approach to take in order to access my logger in my utils?