关于 debug包使用的问题!!!!
发布于 9 年前 作者 flftfqwxf 6344 次浏览 最后一次编辑是 8 年前 来自 问答

新手,debug包不会用!!!

在windows下,试一下官方示例:

var debug = require('debug')('http')
    , http = require('http')
    , name = 'My App';

// fake app

debug('booting %s', name);

http.createServer(function(req, res){
    debug(req.method + req.url);
    res.end('hello\n');
}).listen(3000, function(){
    debug('listening');
});
设置了 debug=*,-not_this

输出的内容:

Mon, 30 Mar 2015 08:33:12 GMT https booting My App
Mon, 30 Mar 2015 08:33:12 GMT https listening
Mon, 30 Mar 2015 08:33:51 GMT https GET/favicon.ico

请问, 1)这个和普通的console.log()有什么区别? 2)require(‘debug’)(‘http’)中的 (‘http’)有什么作用??

7 回复
  1. 我觉得debug可以当成console的wrapper吧.
  2. 类似namesapce
  1. console.log 必须 设置条件或者注释才能 显示或不显示,很麻烦,deubg设置DEBUG环境变量即可
  2. DEBUG环境变量决定输出哪块的信息: export DEBUG=‘http’ (linux 命令), 即输出 ‘http’ 的 信息

简单地说: 可以让你 从 手动 console.log 解脱出来(如果你不想看到console.log还要注释一下),自由决定查看 某个部分的debug信息 (设置DEBUG变量即可)


刚刚才用了debug,回复下

linux 下面设置环境变量可以控制 debug 部分输出,win 下我也不懂怎么设环境变量。。

@alsotang 计算机→属性→高级→环境变量

这个具体要怎么设置,我怎么没有成功,能详细说下吗?

@chenyiqi windows set DEBUG=* && node app.js

@William17 谢谢,可以看到消息了

回到顶部