Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

373
Views
Changing winston logger level in runtime in NestJS app

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!