Console

翻译不完整。 请帮助我们翻译这篇文章!

Console 对象可以接入浏览器控制台(如:Firefox 的 Web Console)。在不同浏览器上它的实现细节可能是不一样的,但这里会介绍一些典型的浏览器会提供的特性。

Console 对象可以从任何全局对象中访问到,如 WindowWorkerGlobalScope 以及控制台属性中的特殊变量。它被定义为 Window.console,而且可直接通过 console 调用。例:

console.log("Failed to open the specified link")

本页面记录了 Console 对象中的 Methods 并给出了几个 Usage 示例。

Note: 此特性在 Web Worker 中可用。

提示: 由于历史原因,console 接口全部被定义为小写(在 i.e. 上不能使用 Console)

方法

Console.assert()
判断第一个参数是否为真,false 的话抛出异常并且在控制台输出相应信息。
Console.clear()
清空控制台,并输出 Console was cleared
Console.count()
以参数为标识记录调用的次数,调用时在控制台打印标识以及调用次数。
Console.countReset()
重置指定标签的计数器值。
Console.debug()
在控制台打印一条 "debug" 级别的消息。
Console.dir()
显示一个由特定的 Javascript 对象列表组成的可交互列表。这个列表可以使用三角形隐藏和显示来审查子对象的内容。.
Console.dirxml()
打印 XML/HTML 元素表示的指定对象,否则显示 JavaScript 对象视图。
Console.error()
打印一条错误信息,使用方法可以参考 string substitution
Console.exception()
error() 方法的别称。
Console.group()
创建一个新的内联 group, 后续所有打印内容将会以子层级的形式展示。调用 groupEnd()来闭合组。
Console.groupCollapsed()
创建一个新的内联 group。使用方法和 group() 相同,不同的是,groupCollapsed() 方法打印出来的内容默认是折叠的。调用groupEnd()来闭合组。
Console.groupEnd()
闭合当前内联 group
Console.info()
打印资讯类说明信息,使用方法可以参考 string substitution
Console.log()
打印内容的通用方法,使用方法可以参考 string substitution
Console.profile()
Starts the browser's built-in profiler (for example, the Firefox performance tool). You can specify an optional name for the profile.
Console.profileEnd()
Stops the profiler. You can see the resulting profile in the browser's performance tool (for example, the Firefox performance tool).
Console.table()
将列表型的数据打印成表格。
Console.time()
启动一个以入参作为特定名称的计时器,在显示页面中可同时运行的计时器上限为10,000.
Console.timeEnd()
结束特定的 计时器 并以豪秒打印其从开始到结束所用的时间。
Console.timeLog()
打印特定 计时器 所运行的时间。
Console.timeStamp()
添加一个标记到浏览器的 Timeline 或 Waterfall 工具。
Console.trace()
输出一个 stack trace
Console.warn()
打印一个警告信息,可以使用 string substitution 和额外的参数。

用法

输出文本到控制台

console 对象中较多使用的主要有四个方法 console.log(), console.info(), console.warn(), 和console.error()。每一个结果在日志中都有不同的样式,可以使用浏览器控制台的日志筛选功能筛选出感兴趣的日志信息。

有两种途径使用这些方法,可以简单的传入一组对象,其中的字符串对象会被连接到一起,输出到控制台。或者可以传入包含零个或多个的替换的字符串,后面跟着被替换的对象列表。

打印单个对象

The simplest way to use the logging methods is to output a single object:

var someObject = { str: "Some text", id: 5 };
console.log(someObject);

打印结果类似下面:

[09:27:13.475] ({str:"Some text", id:5})

打印多个对象

可以打印多个对象,就像下面一样:

var car = "Dodge Charger";
var someObject = { str: "Some text", id: 5 }; 
console.info("My first car was a", car, ". The object is:", someObject);

打印结果类似下面:

[09:28:22.711] My first car was a Dodge Charger . The object is: ({str:"Some text", id:5})

使用字符串替换

可以在传递给 console 的方法的时候使用下面的字符以期进行参数的替换。

Substitution string Description
%o or %O 打印 JavaScript 对象。在审阅器点击对象名字可展开更多对象的信息。
%d or %i 打印整数。支持数字格式化。例如, console.log("Foo %.2d", 1.1) 会输出有先导 0 的两位有效数字: Foo 01
%s 打印字符串。
%f 打印浮点数。支持格式化,比如 console.log("Foo %.2f", 1.1) 会输出两位小数: Foo 1.10

注意:Chrome 不支持精确格式化。

当要替换的参数类型和预期的打印类型不同时,参数会被转换成预期的打印类型。

for (var i=0; i<5; i++) {
  console.log("Hello, %s. You've called me %d times.", "Bob", i+1);
}

输出样例如下所示:

[13:14:13.481] Hello, Bob. You've called me 1 times.
[13:14:13.483] Hello, Bob. You've called me 2 times.
[13:14:13.485] Hello, Bob. You've called me 3 times.
[13:14:13.487] Hello, Bob. You've called me 4 times.
[13:14:13.488] Hello, Bob. You've called me 5 times.

为控制台定义样式

可以使用 %c 为打印内容定义样式:

console.log("This is %cMy stylish message", "color: yellow; font-style: italic; background-color: blue;padding: 2px");
指令前的文本不会受到影响,但指令后的文本将会使用参数中声明的 CSS 样式。


%c 语法可用的属性如下 (至少在 Firefox 中是这样,别的浏览器会有诸多不同):

注意: 控制台信息的默认行为与行内元素相似。为了应用 paddingmargin 这类效果,你应当这样设置display: inline-block.。

在 console 中使用编组

可以使用嵌套组来把视觉上相关的元素合并,以协助组织你的输出。使用console.group()创建新的嵌套块,或者用console.groupCollapsed() 创建默认折叠的块,这种块需要点击闭合按钮来展开才能读到。

直接调用 console.groupEnd().就可以退出当前组。比如下面的代码:

console.log("This is the outer level");
console.group();
console.log("Level 2");
console.group();
console.log("Level 3");
console.warn("More of level 3");
console.groupEnd();
console.log("Back to level 2");
console.groupEnd();
console.debug("Back to the outer level");

执行结果:

Demo of nested groups in Firefox console

定时器

你可以使用定时器来计算一段特定操作的周期。使用 console.time() 方法以创建一个计时器,其唯一的参数表示了计时器的名字。使用 console.timeEnd() 方法以关闭计时器,并获取经过的毫秒数,其同样以计时器的名字作为参数。一个页面最多同时只能有 10,000 个计数器运行。

示例::

console.time("answer time");
alert("Click to continue");
console.timeLog("answer time");
alert("Do a bunch of other stuff...");
console.timeEnd("answer time");

这段代码将会打印需要用户关闭 alert box 的时间, 打印时间到控制台上,等用户关闭第二个 alert 后,把结束时间打印到控制台。

timerresult.png

注意无论在开始还是结束的时候都会打印计时器的名字。

注意: 如果使用计时器来记录网络时间请求的话下面的内容很重要。计时器将会报告传输过程的整个时间,而网络面板里显示的时间只计算了请求头部所需要的时间。如果启用了响应体日志记录,那么列出的响应头部和响应体组合的时间应该与在控制台输出中看到的时间相符。

堆栈跟踪

控制台也支持输出堆栈,其将会显示到调用 console.trace() 的点的调用路径。如下所示:

function foo() {
  function bar() {
    console.trace();
  }
  bar();
}

foo();

控制台的输出:

规范

规范 状态 备注
Console API Living Standard Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
ConsoleChrome Full support 1Edge Full support 12Firefox Full support 2IE Full support 8
Notes
Full support 8
Notes
Notes In Internet Explorer 8 and 9, the console object is undefined when the developer tools are not open. This behavior was fixed in Internet Explorer 10.
Opera Full support 10.5Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 1Samsung Internet Android Full support 1.0
assertChrome Full support 1Edge Full support 12Firefox Full support 28IE Full support 8Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 28Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
clearChrome Full support YesEdge Full support 12Firefox Full support 48IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 48Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
countChrome Full support YesEdge Full support 12Firefox Full support 30IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 30Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
countResetChrome Full support YesEdge Full support ≤79Firefox Full support 62IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 62Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
debugChrome Full support YesEdge Full support 12Firefox Full support 4IE Full support YesOpera ? Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
dir
Experimental
Chrome Full support 1Edge Full support 12Firefox Full support 8IE Full support 9Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 8Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
dirxml
Experimental
Chrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android No support NoOpera Android ? Safari iOS ? Samsung Internet Android Full support Yes
errorChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 8Opera Full support 10.5Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 1Samsung Internet Android Full support 1.0
exception (an alias for error)
DeprecatedNon-standard
Chrome No support NoEdge No support 13 — 79Firefox Full support 28IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 28Opera Android ? Safari iOS No support NoSamsung Internet Android No support No
groupChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 11Opera Full support YesSafari Full support 4WebView Android Full support 37Chrome Android Full support 18Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android Full support 1.0
groupCollapsedChrome Full support 6Edge Full support 12Firefox Full support 52IE Full support 11Opera ? Safari Full support 5.1WebView Android Full support 37Chrome Android Full support 18Firefox Android Full support 52Opera Android ? Safari iOS ? Samsung Internet Android Full support 1.0
groupEndChrome Full support 1Edge Full support 12Firefox Full support 9IE Full support 11Opera Full support YesSafari Full support 4WebView Android Full support 37Chrome Android Full support 18Firefox Android Full support 9Opera Android ? Safari iOS ? Samsung Internet Android Full support 1.0
infoChrome Full support YesEdge Full support 12Firefox Full support 4IE Full support 8Opera Full support YesSafari Full support Yes
Notes
Full support Yes
Notes
Notes No information icon
WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
logChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 8Opera Full support 10.5Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 1Samsung Internet Android Full support 1.0
profile
ExperimentalNon-standard
Chrome Full support 53Edge Full support 12Firefox Full support YesIE Full support YesOpera ? Safari ? WebView Android Full support 53Chrome Android Full support 53Firefox Android Full support 10Opera Android ? Safari iOS ? Samsung Internet Android Full support 6.0
profileEnd
ExperimentalNon-standard
Chrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera ? Safari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 10Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
tableChrome Full support 27Edge Full support 13Firefox Full support 34IE No support NoOpera Full support 11Safari Full support 6.1WebView Android Full support ≤37Chrome Android Full support 27Firefox Android Full support 34Opera Android Full support 11Safari iOS Full support 7Samsung Internet Android Full support 1.5
timeChrome Full support 1Edge Full support 12Firefox Full support 10IE Full support 11Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 10Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
timeEndChrome Full support 1Edge Full support 12Firefox Full support 10IE Full support 11Opera Full support YesSafari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 10Opera Android ? Safari iOS ? Samsung Internet Android Full support 1.0
timeLogChrome Full support 72Edge Full support 79Firefox Full support 62IE No support NoOpera Full support 60Safari No support No
Notes
No support No
Notes
Notes See bug 186833.
WebView Android Full support 72Chrome Android Full support 72Firefox Android Full support 62Opera Android ? Safari iOS No support NoSamsung Internet Android Full support 11.0
timestamp
ExperimentalNon-standard
Chrome Full support YesEdge Full support 12Firefox Full support YesIE Full support 11Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 10Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
traceChrome Full support 1Edge Full support 12Firefox Full support 10IE Full support 11Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 10Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
warnChrome Full support YesEdge Full support 12Firefox Full support 4IE Full support 8Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
Available in workersChrome Full support YesEdge Full support 12Firefox Full support 38IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 38Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.

注意

  • 在 Firefox 浏览器中,如果页面中自己定义了 console 对象,那么它会覆盖掉浏览器内置的 console对象,在其它浏览器可能也是。

相关文档

  • Tools
  • Web Console - Firefox 浏览器控制台如何处理 console API 的调用
  • Remote debugging - 如何在调试移动设备时查看控制台输出。

其他实现