简体   繁体   English

Console.log调试消息管理

[英]Console.log debug messages managing

My JS code is usually full of console.log() debug messages. 我的JS代码通常充满了console.log()调试消息。 Sometimes it is better to turn them off, or to turn off some part of them. 有时最好关掉它们,或者关闭它们的某些部分。

I can, for example, wrap console.log() statement in some function with conditions which are defined by some constants. 例如,我可以在某些函数中包含console.log()语句,条件由一些常量定义。 Is it the best way to manage debug output or are more elegant alternatives? 它是管理调试输出的最佳方式还是更优雅的替代方案?

Bunyan logging module is popular for node.js Bunyan日志记录模块在node.js中很受欢迎

Example code hi.js : 示例代码hi.js

var bunyan = require('bunyan');
var log = bunyan.createLogger({name: 'myapp'});
log.info('hi');
log.warn({lang: 'fr'}, 'au revoir');

Output: 输出:

{"name":"myapp","hostname":"localhost","pid":40161,"level":30,"msg":"hi","time":"2013-01-    04T18:46:23.851Z","v":0}
{"name":"myapp","hostname":"localhost","pid":40161,"level":40,"lang":"fr","msg":"au revoir","time":"2013-01-04T18:46:23.853Z","v":0}

You can then filtering from command lines: 然后,您可以从命令行进行过滤:

$ node hi.js | bunyan -l warn
[2013-01-04T19:08:37.182Z]  WARN: myapp/40353 on localhost: au revoir (lang=fr)

Wrapping console.log into a function works well. console.log包装到函数中效果很好。 But notice that there are also a lot of logging utilities out there for javascript. 但请注意,javascript中还有很多日志实用程序。 A little google on "js logger" may yield suitable results. 关于“js logger”的小谷歌可能会产生合适的结果。

If you're using Node.js then debug is extremely effective as an alternative to console.log() 如果您正在使用Node.js,那么debug作为console.log()的替代方案非常有效

It's basically a substitute for console.log() except you can enable it at the command line with the DEBUG environment variable based on how you've initialized it in each file. 它基本上是console.log()的替代品,除了你可以在命令行使用DEBUG环境变量根据你在每个文件中初始化它的方式启用它。

Let's say I have a project with a couple of files referenced from my index.js file: 假设我有一个项目,其中包含从index.js文件中引用的几个文件:

one.js one.js

var debug = require('debug')('one-one');

var func = function() {
  debug('func');
}

two.js two.js

var debug = require('debug')('one-two');

var func = function() {
  debug('func');
}

You've initialized debug with the name "one-one" in the first file and "one-two" in the second file. 您已在第一个文件中使用名称“one-one”初始化调试,在第二个文件中使用“one-two”初始化调试。

On the command line I can run them like this: 在命令行上我可以像这样运行它们:

node index.js

Result: no debug output. 结果:没有调试输出。 However, if I run it like this: 但是,如果我像这样运行它:

DEBUG=* node index.js

The both the debug statements will get written out, however, in different colors and with the debug name (one-one or one-two) so I can tell which file they came from. 然而,两个调试语句都会以不同的颜色和调试名称(一个或一个两个)写出来,所以我可以告诉它们来自哪个文件。

Now let's say you want to narrow it down a bit more. 现在让我们说你想缩小它的范围。 You could run: 你可以运行:

DEBUG=*-two node index.js DEBUG = * - 两个节点index.js

To only get output from debug that's been set with "-two" at the end of the name or 仅从名称末尾的“-two”设置的调试中获取输出或

DEBUG=one-* node index.js DEBUG = one- * node index.js

to get everything starting with "one-" 从“one-”开始

You can also say that you want everything, or a set of things, or exclude patterns or sets. 您还可以说您想要所有内容,或一组内容,或排除模式或集合。 To exclude something you precede it with a dash, an example: 要使用短划线排除前面的内容,请执行以下示例:

DEBUG=one*,monkey*,-monkey:banana,-elephant,-chimp:* node index.js DEBUG = one *,monkey *, - monkey:banana,-elephant,-chimp:* node index.js

This will include everything starting with "one" or "monkey" and exclude anything called "monkey:banana", or "elephant" or starting with "chimp:" 这将包括以“one”或“monkey”开头的所有内容,并排除任何名为“monkey:banana”或“elephant”或以“chimp:”开头的内容

If you wanted to exclude everything except then: 如果你想排除除此之外的所有内容:

DEBUG=*,-pattern1,-pattern2 node index.js DEBUG = *, - pattern1,-pattern2 node index.js

JS logger是非常好的轻量级工具,具有日志消息级别和几个预定义日志记录级别(DEBUG,INFO,WARN,ERROR)的灵活设置。

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

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