Nodejs-console.error 与 util.debug

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6702456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:23:01  来源:igfitidea点击:

Nodejs- console.error vs util.debug

node.js

提问by beatgammit

I noticed that node.js has both console.errorand util.debug, as well as console.logand util.log.

我注意到 node.js 既有console.errorand util.debug,也有console.logand util.log

Is there a difference besides the console.* functions being more robust in the parameters they take? The API says that they write to stdout and stderr respectively.

除了 console.* 函数在它们采用的参数方面更健壮之外还有什么区别吗?API 表示它们分别写入 stdout 和 stderr。

If there's no difference, which should I use and why?

如果没有区别,我应该使用哪个,为什么?

回答by jcolebrand

They're two different functions, that do two different things. Learn to read the source. It will help you a lot (even in languages like C# with reflector)

它们是两个不同的函数,它们做两件不同的事情。学习阅读源码。它会对你有很大帮助(即使在像 C# 这样的语言中也有反射器)

Sources

来源

Console

安慰

https://github.com/joyent/node/blob/master/lib/console.js

https://github.com/joyent/node/blob/master/lib/console.js

Console.prototype.warn = function() {
  this._stderr.write(util.format.apply(this, arguments) + '\n');
};

Console.prototype.error = Console.prototype.warn;

Utils

实用程序

https://github.com/joyent/node/blob/master/lib/util.js

https://github.com/joyent/node/blob/master/lib/util.js

exports.debug = function(x) {
  process.stderr.write('DEBUG: ' + x + '\n');
};

Log Functions:

日志功能:

Console

安慰

exports.log = function() {
  process.stdout.write(format.apply(this, arguments) + '\n');
};

Utils

实用程序

exports.log = function(msg) {
  exports.puts(timestamp() + ' - ' + msg.toString());
};
exports.puts = function() {
  for (var i = 0, len = arguments.length; i < len; ++i) {
    process.stdout.write(arguments[i] + '\n');
  }
};

Why

为什么

As with every other Unix oriented system, which node is most definitely geared in the guise of - see the many comments from Ryan on the topic, the logging functions are given in the same guise. There are two basic classes for logging, and they both do effectively the same thing, but for different reasons. To the casual observer, they are the same, but they're not really.

与所有其他面向 Unix 的系统一样,哪个节点最明确地以 - 参见 Ryan 关于该主题的许多评论为幌子,日志功能以相同的幌子给出。有两个基本的日志类,它们都有效地做同样的事情,但出于不同的原因。对于普通的观察者来说,它们是相同的,但实际上并非如此。

Console loggingis intended to be used during debugging. This will go to STDOUT and will show your statements on the REPL1console, useful for debugging.

控制台日志记录旨在在调试期间使用。这将转到 STDOUT 并将在 REPL 1控制台上显示您的语句,这对调试很有用。

Utility loggingis intended to be used during standard services runtime. They will goto the processSTDOUT, which often is a log file for the process.

实用程序日志记录旨在在标准服务运行时使用。他们将转到进程STDOUT,它通常是进程的日志文件。

But since this can be outwardly overridden at need, and because it will be different in the future (most likely) for Windows processes (given the new ports and developments) and other systems, then you should try to use these methods as the de facto way to write out to the logs during normal runtime. Examples of how it will be different in Windows include the use of the system log for logging, as opposed to a straight logfile.

但是由于这可以在需要时从外部覆盖,并且因为将来(很可能)对于 Windows 进程(给定新端口和开发)和其他系统会有所不同,那么您应该尝试使用这些方法作为事实上的在正常运行时写入日志的方法。在 Windows 中如何不同的示例包括使用系统日志进行日志记录,而不是直接的日志文件。

So how do you know which one you need?

那么你怎么知道你需要哪一个呢?

If you're planning on running this in the REPL for debugging, use the console logger. If you're intending to start the service and forget about it, then use the utils logging.

如果您计划在 REPL 中运行它进行调试,请使用控制台记录器。如果您打算启动服务并忘记它,请使用 utils 日志记录。

1– Read Evaluate Print Loop

1– 读取评估打印循环

回答by Johann

Joe Lapp's comment to the currently accepted answer deserves to be raised as an answer:

乔拉普对当前接受的答案的评论值得作为答案提出:

FYI: utils.print, .puts, .debug, and .error are now all deprecated. console.log is advised instead of .print and .puts, and console.error is advised instead of .debug and .error. See the utils code

仅供参考:utils.print、.puts、.debug 和 .error 现在都已弃用。建议使用 console.log 代替 .print 和 .puts,建议使用 console.error 代替 .debug 和 .error。查看实用程序代码

References:

参考:

The Node.js Docs

Node.js 文档

The current utils code

当前的utils代码

回答by Ben Taber

The console methods are more robust in that you can pass n arguments and they will be concatenated before being written to stdout or stderr, but they are both convenience wrappers around the same underlying calls to stdout and stderr.

控制台方法更健壮,因为您可以传递 n 个参数,并且它们将在写入 stdout 或 stderr 之前连接起来,但它们都是围绕对 stdout 和 stderr 的相同底层调用的便利包装器。

.log, .info, and .dir are non blocking.
.warn and .error are blocking.

.log、.info 和 .dir 是非阻塞的。
.warn 和 .error 正在阻塞。

console.log and util.log both write using process.stdout.write. console.error and util.debug both write using process.binding("stdio").writeError.

console.log 和 util.log 都使用 process.stdout.write 写入。console.error 和 util.debug 都使用 process.binding("stdio").writeError 写入。