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

167
Vistas
Log4js not write into the file from the catch block

I want to log errors on try catch construct using log4js. I have next code:

let log4js = require('log4js')
log4js.configure({
  appenders: {
    asterisk: {type: 'file', filename: 'filename'},
    out: {type: 'stdout'},
  },
  categories: {default: {appenders: ['asterisk', 'out'], level: 'all'}}
})
const logger = log4js.getLogger('asterisk')
async function myFunc() {
  try {
    let tmp = await someFunc()
  }
  catch (err) {
    logger.error(err)
    process.exit(1)
  }
}
myFunc()

When an error occurs, I see it in the console, but it is not written to the file. If I use logger outside try..catch block then everything works fine. What could be the problem?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As suggested to me on GitHub, the problem is in the process.exit - this causes node to exit before the logger.error call (which is asynchronous behind the scenes) has written to the file. Right code will be like this:

logger.error(err)
logger.shutdown(() => process.exit(1))

and it works fine.

about 4 years ago · Juan Pablo Isaza Denunciar
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