简体   繁体   English

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

[英]How to enable debugging in Node.js application

Currently I'm having a lot of console.log in my modules. 目前我的模块中有很多console.log Many of them are debug-messages, which I only need to see, if I'm debugging my application. 其中许多是调试消息,我只需要查看,如果我正在调试我的应用程序。

I would like to enable debugging using a parameter when starting up my application, for instance node myApp.js -d . 我想在启动我的应用程序时使用参数启用调试,例如node myApp.js -d Further my module should then only call the console.log if debugging is active. 此外,如果调试处于活动状态,我的模块应该只调用console.log

What is best practice in Node to accomplish this? Node中最好的做法是什么?

Use something like winston , as it provides different logging options. 使用类似winston的东西,因为它提供了不同的日志选项。 Below is a simple example of something u may need 以下是您可能需要的一个简单示例

// 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!');

Look up npm debug . 查找npm debug I believe that's what you are looking for. 我相信这就是你要找的东西。

To Install debug: 安装调试:

 npm install debug --save

example code: 示例代码:

var debug = require('debug')('your module');
debug('msg', 'more details');

To start debugging all the application run the following: 要开始调试所有应用程序,请运行以下命令:

DEBUG=* node myApp.js

To debug only a few modules on your app: 要仅调试应用程序上的几个模块:

DEBUG=first,second node myApp.js

For no debugging run your app normally 没有调试正常运行你的应用程序

node myApp.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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