[英]How to enable debugging in Node.js application
目前我的模塊中有很多console.log
。 其中許多是調試消息,我只需要查看,如果我正在調試我的應用程序。
我想在啟動我的應用程序時使用參數啟用調試,例如node myApp.js -d
。 此外,如果調試處於活動狀態,我的模塊應該只調用console.log
。
Node中最好的做法是什么?
使用類似winston的東西,因為它提供了不同的日志選項。 以下是您可能需要的一個簡單示例
// Require winston logging library
var winston = require('winston');
// Check if -d is present as CLI argument
if(process.argv.indexOf('-d') > -1){
winston.level = 'debug';
}
// This is only print when winston.level is debug
winston.log('debug', 'Now my debug messages are written to console!');
查找npm debug 。 我相信這就是你要找的東西。
安裝調試:
npm install debug --save
示例代碼:
var debug = require('debug')('your module');
debug('msg', 'more details');
要開始調試所有應用程序,請運行以下命令:
DEBUG=* node myApp.js
要僅調試應用程序上的幾個模塊:
DEBUG=first,second node myApp.js
沒有調試正常運行你的應用程序
node myApp.js
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.