Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

370
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda