Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

372
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda