uncaughtException然后appendFile错误会导致问题? 的NodeJS

我很好奇,如果因为process.exit(1)是在asynchronous操作的callback,appendFile,如果原来的错误发生的地方将继续运行,并处于不稳定的状态。 我试图把process.exit(1)与fs内联,但不会写入。

更好的方法来创build一个错误logging,将login任何错误,将不胜感激。 。

const fs = require('fs'); process.on('uncaughtException', e1 => { fs.appendFile('./logs/error.log', e1.stack, e2 => { //if (e1 || e2) console.log(e1,e2); process.exit(1) }); }) 

fs.appendFile ,看起来像你的程序将退出一个错误代码到控制台每次完成fs.appendFileprocess.exit应该杀死其他任何运行一旦它击中。

我可能会做这样的事情:

 const fs = require('fs'); process.on('uncaughtException', function (err) { fs.appendFile('./logs/error.log', err.stack, (fserr) => { if (err) console.log('FS ERROR', fserr); //now includes the fs error in log console.log(err); //maybe include error from uncaughtException? process.exit(1); //exit with error code }); });