繁体   English   中英

如何在Node.js应用程序中启用调试

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM