简体   繁体   English

console.log() 和 console.debug() 的区别?

[英]Difference between console.log() and console.debug()?

Google has not been helpful for me, since searching for "console.debug" just brings up a bunch of pages that have the words "console" and "debug" on them.谷歌对我没有帮助,因为搜索“console.debug”只会显示一堆带有“console”和“debug”字样的页面。

I'm wondering what the difference is between console.log() and console.debug() .我想知道console.log()console.debug()之间有什么区别。 Is there some way to use a bunch of console.debug() statements and then just flip a switch to easily shut off all debug statements from being sent to the console (like after launching a site)?有什么方法可以使用一堆console.debug()语句,然后只需翻转一个开关即可轻松关闭所有调试语句,使其不被发送到控制台(例如在启动站点之后)?

Technically console.log console.debug and console.info are identical However the way they display the data is little different 从技术上讲, console.log console.debugconsole.info是相同的但是它们显示数据的方式却略有不同

console.log Black color text with no icon console.log没有图标的黑色文本

console.info Blue color text with icon console.info带图标的蓝色文本

console.debug Pure black color text console.debug纯黑色文字

console.warn Yellow color text with icon console.warn带图标的黄色文本

console.error Red Color text with icon console.error带图标的红色文本

var playerOne = 120;
var playerTwo = 130;
var playerThree = 140;
var playerFour = 150;
var playerFive = 160;

console.log("Console.log" + " " +  playerOne);
console.debug("Console.debug" + " " +playerTwo);
console.warn("Console.warn" + " " + playerThree);
console.info("Console.info" + " " + playerFour);
console.error("Console.error" + " " + playerFive);

在此输入图像描述

它们几乎完全相同 - 唯一的区别是默认情况下调试消息在最近版本的Chrome中隐藏(您必须在Devtools顶部栏中将日志级别设置为Verbose ,而在控制台中查看调试消息;默认情况下可以看到日志消息) 。

console.info , console.debug methods are identical to console.log . console.infoconsole.debug方法与console.log相同。

  • console.log Printing statement console.log 打印声明
  • console.info Black color text with "i" icon in blue color console.info黑色文本,蓝色“i”图标
  • console.debug Blue Color text console.debug蓝色文本

Documentation: 文档:

If you want the ability to disable logging after a product is finished you could override the console.debug() function or make another custom one. 如果您希望能够在产品完成后禁用日志记录,则可以覆盖console.debug()函数或创建另一个自定义日志。

console.debug = function() {
    if(!console.debugging) return;
    console.log.apply(this, arguments);
};

console.debugging = true;
console.debug('Foo', {age:41, name:'Jhon Doe'});

Foo▸ {age: 41, name: "Jhon Doe"} Foo▸{年龄:41,名字:“Jhon Doe”}

console.debugging = false;
console.debug('Foo', {age:26, name:'Jane Doe'});

No output 没有输出

However I havent figured a way to color the outputs as well. 但是我还没有找到一种方法来为输出着色。

From Documentation of browsers,The log , debug and also info methods are identical in implementation wise but varies in color and icon 从浏览器的文档中, logdebuginfo方法在实现方面是相同的,但颜色和图标各不相同

https://jsfiddle.net/yp4z76gg/1/ https://jsfiddle.net/yp4z76gg/1/

I know it's old, but to continue on @Espen's answer, you can separate responsibilities and use console.log for your regular logs that should also appear on production, and for console.debug you can do something like that:我知道它已经过时了,但是要继续@Espen 的回答,您可以分离职责并将 console.log 用于您的常规日志,这些日志也应该出现在生产中,对于 console.debug 您可以执行以下操作:

if (env === 'production') {
   console.debug = function () {};
}

This will override console.debug and it won't print anything.这将覆盖 console.debug 并且不会打印任何内容。 This way you don't have to worry about things that should appear in prod environment.这样您就不必担心应该出现在 prod 环境中的东西。

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

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