2011-11-01 44 views
2

我開始我的node.js服務器這樣的:node.js的錯誤日誌包括更多信息

須藤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) 

如何讓我的錯誤日誌顯示的詳細信息,如採取了錯誤發生的日期時間?

+0

我想你的玉石版本可能已經過時了 - 「npm ls」是什麼意思? – thejh

+0

@thejh只是一個例子,實際上並沒有試圖調試err,儘管 – Harry

回答

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_

+0

我怎樣才能讓它記錄堆棧跟蹤? – Harry

+0

console.log(err.stack) –

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()添加到上面未捕獲的異常處理程序中可能是明智的。

相關問題