Console API

From FirebugWiki

(Difference between revisions)
Jump to: navigation, search
(Fixed implementation notes, removed obsolete historical info)
(Described %.xf)
(5 intermediate revisions not shown)
Line 4: Line 4:
=== console.log(object[, object, ...]) ===
=== console.log(object[, object, ...]) ===
-
Writes a message to the console. You may pass as many arguments as you'd like, and they will be joined together in a space-delimited line.
+
Writes a message to the console. You may pass as many arguments as you'd like, and they will be joined together in a space-delimited line.
-
The first argument to <code>log</code> may be a string containing printf-like string substitution patterns. For example:
+
The first argument to <code>log</code> may be a string containing printf-like string substitution patterns. For example:
 +
 
 +
<source lang="javascript">
 +
console.log("The %s jumped over %d tall buildings", animal, count);
 +
</source>
-
<pre class="code">console.log("The %s jumped over %d tall buildings", animal, count);</pre>
 
The example above can be re-written without string substitution to achieve the same result:
The example above can be re-written without string substitution to achieve the same result:
-
<pre class="code">console.log("The", animal, "jumped over", count, "tall buildings");</pre>
+
<source lang="javascript">
-
These two techniques can be combined. If you use string substitution but provide more arguments than there are substitution patterns, the remaining arguments will be appended in a space-delimited line, like so:
+
console.log("The", animal, "jumped over", count, "tall buildings");
 +
</source>
 +
 
 +
These two techniques can be combined. If you use string substitution but provide more arguments than there are substitution patterns, the remaining arguments will be appended in a space-delimited line, like so:
 +
 
 +
<source lang="javascript">
 +
console.log("I am %s and I have:", myName, thing1, thing2, thing3);
 +
</source>
 +
 
 +
If objects are logged, they will be written not as static text, but as interactive hyperlinks that can be clicked to inspect the object in Firebug's HTML, CSS, Script, or DOM tabs.  You may also use the <code>%o</code> pattern to substitute a hyperlink in a string.
 +
 
 +
You may also use the <code>%c</code> pattern to use the second argument as a style formatting parameter. For example:
-
<pre class="code">console.log("I am %s and I have:", myName, thing1, thing2, thing3);</pre>
+
<source lang="javascript">
-
If objects are logged, they will be written not as static text, but as interactive hyperlinks that can be clicked to inspect the object in Firebug's HTML, CSS, Script, or DOM tabs.  You may also use the %o pattern to substitute a hyperlink in a string.
+
console.log("%cThis is green text on a yellow background.", "color:green; background-color:yellow");
 +
</source>
-
You may also use the %c pattern to use the second argument as a style formatting parameter. For example:
+
You can even use several <code>%c</code> patterns within the string to use different formattings. For each <code>%c</code> there needs to be one argument:
-
<pre class="code">console.log('%cThis is red text on a green background', 'color:red; background-color:green');</pre>
+
<source lang="javascript">
 +
console.log("%cRed text, %cgreen text, %cblue text", "color:red", "color:green", "color:blue");
 +
</source>
Here is the complete set of patterns that you may use for string substitution:
Here is the complete set of patterns that you may use for string substitution:
-
{|
+
{| class="wikitable"
|-
|-
! Pattern !! Type
! Pattern !! Type
|-
|-
-
| %s || String
+
| <code>%s</code> || String
|-
|-
-
| %d, %i || Integer (numeric formatting is not yet supported)
+
| <code>%d</code>, <code>%i</code> || Integer (numeric formatting is not yet supported)
|-
|-
-
| %f || Floating point number (numeric formatting is not yet supported)
+
| <code>%f</code>/<code>%.''x''f</code> || Floating point number; ''x'' denotes the number of decimal places the number should be rounded to (if ommitted, the number won't be rounded)
|-
|-
-
| %o || Object hyperlink
+
| <code>%o</code> || Object hyperlink
|-
|-
-
| %c || Style formatting
+
| <code>%c</code> || Style formatting
|}
|}
Line 81: Line 98:
=== console.timeEnd(name) ===
=== console.timeEnd(name) ===
Stops a timer created by a call to <code>console.time(name)</code> and writes the time elapsed.
Stops a timer created by a call to <code>console.time(name)</code> and writes the time elapsed.
 +
 +
=== console.timeStamp(name) ===
 +
Creates a time stamp, which can be used together with [[Net Panel#Request Timeline|HTTP traffic timing]] to measure when a certain piece of code was executed.
=== console.profile([title]) ===
=== console.profile([title]) ===
Line 95: Line 115:
=== console.table(data[, columns]) ===
=== console.table(data[, columns]) ===
-
<p>Allows to log provided data using tabular layout. The method takes one required parameter that represents table like data (array of arrays or list of objects). The other optional parameter can be used to specify columns and/or properties to be logged (see more [http://www.softwareishard.com/blog/firebug/tabular-logs-in-firebug/ here]).</p>
+
Allows to log provided data using tabular layout. The method takes one required parameter that represents table-like data (array of arrays or list of objects). The optional <code>columns</code> parameter can be used to specify columns and/or properties to be logged (see more at [http://www.softwareishard.com/blog/firebug/tabular-logs-in-firebug/ softwareishard.com]).
== Implementation Notes ==
== Implementation Notes ==
-
The <code>console</code> is an object attached to the <code>window</code> object in the web page.
+
* The <code>console</code> is an object attached to the <code>window</code> object in the web page.
-
 
+
* If the page already contains a <code>console</code> variable, it is '''not''' overwritten by Firebug's Console API.
-
In Firebug for Firefox the object is attached only if the Console panel is enabled, and it is injected just before the first JavaScript script tag is run. So the first script tag is compiled, then the console is injected, then the outer function code of the script tag is executed. As a consequence, Firebug might not always catch logs done by e.g. Greasemonkey scripts. [http://code.google.com/p/fbug/issues/detail?id=4978 This might be fixed in a future version.]
+
* In Firebug for Firefox the object is attached only if the [[Console Panel]] is enabled, and it is injected just before the first JavaScript script tag is run. So the first script tag is compiled, then the console is injected, then the outer function code of the script tag is executed. As a consequence, Firebug might not always catch logs done by e.g. Greasemonkey scripts. [http://code.google.com/p/fbug/issues/detail?id=4978 This might be fixed in a future version.]
 +
* In [http://getfirebug.com/firebuglite Firebug Lite], the console is attached as soon as Lite is installed into the page.
-
In [http://getfirebug.com/firebuglite Firebug Lite], the console is attached as soon as Lite is installed into the page.
+
== Note ==
 +
The console API formerly implemented a <code>console.firebug</code> property. This property was removed from the API in Firebug 1.9.0 in order to prevent sites from detecting whether a user has Firebug installed.
== See also ==
== See also ==
* [[Console]]
* [[Console]]
* [[Command Line API]]
* [[Command Line API]]

Revision as of 21:34, 21 August 2013

Firebug adds a global variable named "console" to all web pages loaded in Firefox. This object contains many methods that allow you to write to the Firebug console to expose information that is flowing through your scripts.

Contents

Commands

console.log(object[, object, ...])

Writes a message to the console. You may pass as many arguments as you'd like, and they will be joined together in a space-delimited line.

The first argument to log may be a string containing printf-like string substitution patterns. For example:

console.log("The %s jumped over %d tall buildings", animal, count);

The example above can be re-written without string substitution to achieve the same result:

console.log("The", animal, "jumped over", count, "tall buildings");

These two techniques can be combined. If you use string substitution but provide more arguments than there are substitution patterns, the remaining arguments will be appended in a space-delimited line, like so:

console.log("I am %s and I have:", myName, thing1, thing2, thing3);

If objects are logged, they will be written not as static text, but as interactive hyperlinks that can be clicked to inspect the object in Firebug's HTML, CSS, Script, or DOM tabs. You may also use the %o pattern to substitute a hyperlink in a string.

You may also use the %c pattern to use the second argument as a style formatting parameter. For example:

console.log("%cThis is green text on a yellow background.", "color:green; background-color:yellow");

You can even use several %c patterns within the string to use different formattings. For each %c there needs to be one argument:

console.log("%cRed text, %cgreen text, %cblue text", "color:red", "color:green", "color:blue");

Here is the complete set of patterns that you may use for string substitution:

Pattern Type
%s String
%d, %i Integer (numeric formatting is not yet supported)
%f/%.xf Floating point number; x denotes the number of decimal places the number should be rounded to (if ommitted, the number won't be rounded)
%o Object hyperlink
%c Style formatting

console.debug(object[, object, ...])

Writes a message to the console, including a hyperlink to the line where it was called.

console.info(object[, object, ...])

Writes a message to the console with the visual "info" icon and color coding and a hyperlink to the line where it was called.

console.warn(object[, object, ...])

Writes a message to the console with the visual "warning" icon and color coding and a hyperlink to the line where it was called.

console.error(object[, object, ...])

Writes a message to the console with the visual "error" icon and color coding and a hyperlink to the line where it was called.

console.assert(expression[, object, ...])

Tests that an expression is true. If not, it will write a message to the console and throw an exception.

console.clear()

Clears the console.

console.dir(object)

Prints an interactive listing of all properties of the object. This looks identical to the view that you would see in the DOM tab.

console.dirxml(node)

Prints the XML source tree of an HTML or XML element. This looks identical to the view that you would see in the HTML tab. You can click on any node to inspect it in the HTML tab.

console.trace()

Prints an interactive stack trace of JavaScript execution at the point where it is called.

The stack trace details the functions on the stack, as well as the values that were passed as arguments to each function. You can click each function to take you to its source in the Script tab, and click each argument value to inspect it in the DOM or HTML tabs.

console.group(object[, object, ...])

Writes a message to the console and opens a nested block to indent all future messages sent to the console. Call console.groupEnd() to close the block.

console.groupCollapsed(object[, object, ...])

Like console.group(), but the block is initially collapsed.

console.groupEnd()

Closes the most recently opened block created by a call to console.group() or console.groupCollapsed()

console.time(name)

Creates a new timer under the given name. Call console.timeEnd(name) with the same name to stop the timer and print the time elapsed..

console.timeEnd(name)

Stops a timer created by a call to console.time(name) and writes the time elapsed.

console.timeStamp(name)

Creates a time stamp, which can be used together with HTTP traffic timing to measure when a certain piece of code was executed.

console.profile([title])

Turns on the JavaScript profiler. The optional argument title would contain the text to be printed in the header of the profile report.

console.profileEnd()

Turns off the JavaScript profiler and prints its report.

console.count([title])

Writes the number of times that the line of code where count was called was executed. The optional argument title will print a message in addition to the number of the count.

console.exception(error-object[, object, ...])

Prints an error message together with an interactive stack trace of JavaScript execution at the point where the exception occurred.

console.table(data[, columns])

Allows to log provided data using tabular layout. The method takes one required parameter that represents table-like data (array of arrays or list of objects). The optional columns parameter can be used to specify columns and/or properties to be logged (see more at softwareishard.com).

Implementation Notes

  • The console is an object attached to the window object in the web page.
  • If the page already contains a console variable, it is not overwritten by Firebug's Console API.
  • In Firebug for Firefox the object is attached only if the Console Panel is enabled, and it is injected just before the first JavaScript script tag is run. So the first script tag is compiled, then the console is injected, then the outer function code of the script tag is executed. As a consequence, Firebug might not always catch logs done by e.g. Greasemonkey scripts. This might be fixed in a future version.
  • In Firebug Lite, the console is attached as soon as Lite is installed into the page.

Note

The console API formerly implemented a console.firebug property. This property was removed from the API in Firebug 1.9.0 in order to prevent sites from detecting whether a user has Firebug installed.

See also

Personal tools