node.js错误日志包含更多信息

时间:2011-11-01 05:52:13

标签: javascript debugging node.js

我正在启动我的node.js服务器:

sudo NODE_ENV =生产永远开始-o out.log -e err.log server.js

有时我的错误日志会出错。

但我得到的是这样的:

Cannot read property 'length' of undefined
    at eval at <anonymous> (/home/ubuntu/www/node_modules/jade/lib/jade.js:161:8)
    at anonymous (eval at <anonymous> (/home/ubuntu/www/node_modules/jade/lib/jade.js:161:8))
    at Object.<anonymous> (/home/ubuntu/www/node_modules/jade/lib/jade.js:166:12)
    at ServerResponse._render (/home/ubuntu/www/node_modules/express/lib/view.js:422:21)
    at ServerResponse.render (/home/ubuntu/www/node_modules/express/lib/view.js:315:17)
    at callbacks (/home/ubuntu/www/node_modules/express/lib/router/index.js:272:11)
    at Promise.<anonymous> (/home/ubuntu/www/helper/auth.js:118:11)
    at Promise.<anonymous> (/home/ubuntu/www/node_modules/mongoose/lib/promise.js:120:8)

如何让错误日志显示更多信息,例如错误发生的日期时间?

2 个答案:

答案 0 :(得分:3)

使用'uncaughtException'事件,如下所示:

process.on('uncaughtException', function (err) {
  console.log('Caught exception: ' + err);
});

setTimeout(function () {
  console.log('This will still run.');
}, 500);

// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
console.log('This will not run.');

比您可以记录日期以及您想要在回调中注销的任何其他内容。

更多关于流程的信息: http://nodejs.org/docs/v0.4.12/api/process.html#event_uncaughtException_

答案 1 :(得分:1)

如果您需要日期时间信息,可以使用require('util').log()

进行打印

如果您还想阻止您的应用程序退出,您可以捕获未捕获的异常

var util = require('util');

process.on('uncaughtException', function (err) {
//Traceout the error details    
console.error(err.stack)
  util.log('Caught exception: ' + err.message);
});

请记住,如果抛出异常,则可能已经运行的回调序列已停止,因此如果继续运行它,您的应用程序可能会有奇怪的行为。将process.exit()添加到上面未捕获的异常处理程序中可能是明智的。

相关问题