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

165
Views
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 answers
Answer question

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 Report
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!