I need to change winston level with my REST endpoint. I have created a logger service like this:
@Injectable()
export class CustomLoggerService implements LoggerService {
private logger: Logger
constructor() {
this.logger = createLogger(this.getLoggerInstance())
}
getLoggerInstance(customLevel?: string): LoggerOptions {
const console = new transports.Console({
level: customLevel || 'info',
format: format.combine(
format.timestamp(),
format.json(),
),
})
return {
exitOnError: false,
handleExceptions: true,
transports: [console],
}
}
// THIS METHOD DOESN'T WORK
updateLevel(level: string): void {
this.logger = createLogger(this.getLoggerInstance(level))
// I also tried edit level in transports directly but not helps
// this.logger.transports.forEach(t => (t.level = level))
}
// implemented log messages
}
This logger I am using then in boostrap of application like this:
const app = await NestFactory.create(AppModule, { logger: true })
const logger = app.get(CustomLoggerService)
app.useLogger(logger)
App is internally using my logger, and everything is ok, but I am not able to change level in runtime.
In my providers I am using logger like this:
@Controller('foo')
export class FooController{
private readonly logger = new Logger(FooController.name)
// I also tried inject it with DI like this but same behavior
// constructor(private readonly logger: CustomLoggerService) {}
@Get()
async index(): Promise<void> {
this.logger.log('This I am able to see in console')
this.logger.log('This I am NOT able to see in console')
}
}
Do you have idea what is wrong with code above? Thank you.