1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Console | Node.js v4.8.2 Manual & Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/sh.css">
<link rel="canonical" href="https://nodejs.org/api/console.html">
</head>
<body class="alt apidoc" id="api-section-console">
<div id="content" class="clearfix">
<div id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a class="nav-documentation" href="documentation.html">About these Docs</a></li>
<li><a class="nav-synopsis" href="synopsis.html">Usage & Example</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a class="nav-assert" href="assert.html">Assertion Testing</a></li>
<li><a class="nav-buffer" href="buffer.html">Buffer</a></li>
<li><a class="nav-addons" href="addons.html">C/C++ Addons</a></li>
<li><a class="nav-child_process" href="child_process.html">Child Processes</a></li>
<li><a class="nav-cluster" href="cluster.html">Cluster</a></li>
<li><a class="nav-cli" href="cli.html">Command Line Options</a></li>
<li><a class="nav-console active" href="console.html">Console</a></li>
<li><a class="nav-crypto" href="crypto.html">Crypto</a></li>
<li><a class="nav-debugger" href="debugger.html">Debugger</a></li>
<li><a class="nav-dns" href="dns.html">DNS</a></li>
<li><a class="nav-domain" href="domain.html">Domain</a></li>
<li><a class="nav-errors" href="errors.html">Errors</a></li>
<li><a class="nav-events" href="events.html">Events</a></li>
<li><a class="nav-fs" href="fs.html">File System</a></li>
<li><a class="nav-globals" href="globals.html">Globals</a></li>
<li><a class="nav-http" href="http.html">HTTP</a></li>
<li><a class="nav-https" href="https.html">HTTPS</a></li>
<li><a class="nav-modules" href="modules.html">Modules</a></li>
<li><a class="nav-net" href="net.html">Net</a></li>
<li><a class="nav-os" href="os.html">OS</a></li>
<li><a class="nav-path" href="path.html">Path</a></li>
<li><a class="nav-process" href="process.html">Process</a></li>
<li><a class="nav-punycode" href="punycode.html">Punycode</a></li>
<li><a class="nav-querystring" href="querystring.html">Query Strings</a></li>
<li><a class="nav-readline" href="readline.html">Readline</a></li>
<li><a class="nav-repl" href="repl.html">REPL</a></li>
<li><a class="nav-stream" href="stream.html">Stream</a></li>
<li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li>
<li><a class="nav-timers" href="timers.html">Timers</a></li>
<li><a class="nav-tls" href="tls.html">TLS/SSL</a></li>
<li><a class="nav-tty" href="tty.html">TTY</a></li>
<li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li>
<li><a class="nav-url" href="url.html">URL</a></li>
<li><a class="nav-util" href="util.html">Utilities</a></li>
<li><a class="nav-v8" href="v8.html">V8</a></li>
<li><a class="nav-vm" href="vm.html">VM</a></li>
<li><a class="nav-zlib" href="zlib.html">ZLIB</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">GitHub Repo & Issue Tracker</a></li>
<li><a class="nav-http-groups-google-com-group-nodejs" href="http://groups.google.com/group/nodejs">Mailing List</a></li>
</ul>
</div>
<div id="column1" data-id="console" class="interior">
<header>
<h1>Node.js v4.8.2 Documentation</h1>
<div id="gtoc">
<p>
<a href="index.html" name="toc">Index</a> |
<a href="all.html">View on single page</a> |
<a href="console.json">View as JSON</a>
</p>
</div>
<hr>
</header>
<div id="toc">
<h2>Table of Contents</h2>
<ul>
<li><span class="stability_2"><a href="#console_console">Console</a></span><ul>
<li><span class="stability_undefined"><a href="#console_asynchronous_vs_synchronous_consoles">Asynchronous vs Synchronous Consoles</a></span></li>
<li><span class="stability_undefined"><a href="#console_class_console">Class: Console</a></span><ul>
<li><span class="stability_undefined"><a href="#console_new_console_stdout_stderr">new Console(stdout[, stderr])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_assert_value_message">console.assert(value[, message][, ...])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_dir_obj_options">console.dir(obj[, options])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_error_data">console.error([data][, ...])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_info_data">console.info([data][, ...])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_log_data">console.log([data][, ...])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_time_label">console.time(label)</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_timeend_label">console.timeEnd(label)</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_trace_message">console.trace(message[, ...])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_warn_data">console.warn([data][, ...])</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div id="apicontent">
<h1>Console<span><a class="mark" href="#console_console" id="console_console">#</a></span></h1>
<pre class="api_stability api_stability_2">Stability: 2 - Stable</pre><p>The <code>console</code> module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.</p>
<p>The module exports two specific components:</p>
<ul>
<li>A <code>Console</code> class with methods such as <code>console.log()</code>, <code>console.error()</code> and
<code>console.warn()</code> that can be used to write to any Node.js stream.</li>
<li>A global <code>console</code> instance configured to write to <code>stdout</code> and <code>stderr</code>.
Because this object is global, it can be used without calling
<code>require('console')</code>.</li>
</ul>
<p>Example using the global <code>console</code>:</p>
<pre><code class="lang-js">console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to stderr
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
</code></pre>
<p>Example using the <code>Console</code> class:</p>
<pre><code class="lang-js">const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
</code></pre>
<p>While the API for the <code>Console</code> class is designed fundamentally around the
browser <code>console</code> object, the <code>Console</code> in Node.js is <em>not</em> intended to
duplicate the browser's functionality exactly.</p>
<h2>Asynchronous vs Synchronous Consoles<span><a class="mark" href="#console_asynchronous_vs_synchronous_consoles" id="console_asynchronous_vs_synchronous_consoles">#</a></span></h2>
<p>The console functions are usually asynchronous unless the destination is a file.
Disks are fast and operating systems normally employ write-back caching;
it should be a very rare occurrence indeed that a write blocks, but it
is possible.</p>
<p>Additionally, console functions are blocking when outputting to TTYs
(terminals) on OS X as a workaround for the OS's very small, 1kb buffer size.
This is to prevent interleaving between <code>stdout</code> and <code>stderr</code>.</p>
<h2>Class: Console<span><a class="mark" href="#console_class_console" id="console_class_console">#</a></span></h2>
<!--type=class-->
<p>The <code>Console</code> class can be used to create a simple logger with configurable
output streams and can be accessed using either <code>require('console').Console</code>
or <code>console.Console</code>:</p>
<pre><code class="lang-js">const Console = require('console').Console;
const Console = console.Console;
</code></pre>
<h3>new Console(stdout[, stderr])<span><a class="mark" href="#console_new_console_stdout_stderr" id="console_new_console_stdout_stderr">#</a></span></h3>
<p>Creates a new <code>Console</code> by passing one or two writable stream instances.
<code>stdout</code> is a writable stream to print log or info output. <code>stderr</code>
is used for warning or error output. If <code>stderr</code> isn't passed, warning and error
output will be sent to <code>stdout</code>.</p>
<pre><code class="lang-js">const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
const count = 5;
logger.log('count: %d', count);
// in stdout.log: count 5
</code></pre>
<p>The global <code>console</code> is a special <code>Console</code> whose output is sent to
<a href="process.html#process_process_stdout"><code>process.stdout</code></a> and <a href="process.html#process_process_stderr"><code>process.stderr</code></a>. It is equivalent to calling:</p>
<pre><code class="lang-js">new Console(process.stdout, process.stderr);
</code></pre>
<h3>console.assert(value[, message][, ...])<span><a class="mark" href="#console_console_assert_value_message" id="console_console_assert_value_message">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.101</span>
</div><p>A simple assertion test that verifies whether <code>value</code> is truthy. If it is not,
an <code>AssertionError</code> is thrown. If provided, the error <code>message</code> is formatted
using <a href="util.html#util_util_format_format"><code>util.format()</code></a> and used as the error message.</p>
<pre><code class="lang-js">console.assert(true, 'does nothing');
// OK
console.assert(false, 'Whoops %s', 'didn\'t work');
// AssertionError: Whoops didn't work
</code></pre>
<p><em>Note: the <code>console.assert()</code> method is implemented differently in Node.js
than the <code>console.assert()</code> method <a href="https://developer.mozilla.org/en-US/docs/Web/API/console/assert">available in browsers</a>.</em></p>
<p>Specifically, in browsers, calling <code>console.assert()</code> with a falsy
assertion will cause the <code>message</code> to be printed to the console without
interrupting execution of subsequent code. In Node.js, however, a falsy
assertion will cause an <code>AssertionError</code> to be thrown.</p>
<p>Functionality approximating that implemented by browsers can be implemented
by extending Node.js' <code>console</code> and overriding the <code>console.assert()</code> method.</p>
<p>In the following example, a simple module is created that extends and overrides
the default behavior of <code>console</code> in Node.js.</p>
<pre><code class="lang-js">'use strict';
// Creates a simple extension of console with a
// new impl for assert without monkey-patching.
const myConsole = Object.create(console, {
assert: {
value: function assert(assertion, message, ...args) {
try {
console.assert(assertion, message, ...args);
} catch (err) {
console.error(err.stack);
}
},
configurable: true,
enumerable: true,
writable: true,
},
});
module.exports = myConsole;
</code></pre>
<p>This can then be used as a direct replacement for the built in console:</p>
<pre><code class="lang-js">const console = require('./myConsole');
console.assert(false, 'this message will print, but no error thrown');
console.log('this will also print');
</code></pre>
<h3>console.dir(obj[, options])<span><a class="mark" href="#console_console_dir_obj_options" id="console_console_dir_obj_options">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.101</span>
</div><p>Uses <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> on <code>obj</code> and prints the resulting string to <code>stdout</code>.
This function bypasses any custom <code>inspect()</code> function defined on <code>obj</code>. An
optional <code>options</code> object may be passed to alter certain aspects of the
formatted string:</p>
<ul>
<li><p><code>showHidden</code> - if <code>true</code> then the object's non-enumerable and symbol
properties will be shown too. Defaults to <code>false</code>.</p>
</li>
<li><p><code>depth</code> - tells <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> how many times to recurse while
formatting the object. This is useful for inspecting large complicated objects.
Defaults to <code>2</code>. To make it recurse indefinitely, pass <code>null</code>.</p>
</li>
<li><p><code>colors</code> - if <code>true</code>, then the output will be styled with ANSI color codes.
Defaults to <code>false</code>. Colors are customizable; see
<a href="util.html#util_customizing_util_inspect_colors">customizing <code>util.inspect()</code> colors</a>.</p>
</li>
</ul>
<h3>console.error([data][, ...])<span><a class="mark" href="#console_console_error_data" id="console_console_error_data">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>Prints to <code>stderr</code> with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html">printf(3)</a> (the arguments are all passed to
<a href="util.html#util_util_format_format"><code>util.format()</code></a>).</p>
<pre><code class="lang-js">const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr
</code></pre>
<p>If formatting elements (e.g. <code>%d</code>) are not found in the first string then
<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> is called on each argument and the resulting string
values are concatenated. See <a href="util.html#util_util_format_format"><code>util.format()</code></a> for more information.</p>
<h3>console.info([data][, ...])<span><a class="mark" href="#console_console_info_data" id="console_console_info_data">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>The <code>console.info()</code> function is an alias for <a href="#console_console_log_data"><code>console.log()</code></a>.</p>
<h3>console.log([data][, ...])<span><a class="mark" href="#console_console_log_data" id="console_console_log_data">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>Prints to <code>stdout</code> with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html">printf(3)</a> (the arguments are all passed to
<a href="util.html#util_util_format_format"><code>util.format()</code></a>).</p>
<pre><code class="lang-js">const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
</code></pre>
<p>If formatting elements (e.g. <code>%d</code>) are not found in the first string then
<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> is called on each argument and the resulting string
values are concatenated. See <a href="util.html#util_util_format_format"><code>util.format()</code></a> for more information.</p>
<h3>console.time(label)<span><a class="mark" href="#console_console_time_label" id="console_console_time_label">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.104</span>
</div><p>Used to calculate the duration of a specific operation. To start a timer, call
the <code>console.time()</code> method, giving it a unique <code>label</code> as the only parameter. To stop the
timer, and to get the elapsed time in milliseconds, just call the
<a href="#console_console_timeend_label"><code>console.timeEnd()</code></a> method, again passing the
timer's unique <code>label</code> as the parameter.</p>
<h3>console.timeEnd(label)<span><a class="mark" href="#console_console_timeend_label" id="console_console_timeend_label">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.104</span>
</div><p>Stops a timer that was previously started by calling <a href="#console_console_time_label"><code>console.time()</code></a> and
prints the result to stdout:</p>
<pre><code class="lang-js">console.time('100-elements');
for (let i = 0; i < 100; i++) {
;
}
console.timeEnd('100-elements');
// prints 100-elements: 262ms
</code></pre>
<h3>console.trace(message[, ...])<span><a class="mark" href="#console_console_trace_message" id="console_console_trace_message">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.104</span>
</div><p>Prints to <code>stderr</code> the string <code>'Trace :'</code>, followed by the <a href="util.html#util_util_format_format"><code>util.format()</code></a>
formatted message and stack trace to the current position in the code.</p>
<pre><code class="lang-js">console.trace('Show me');
// Prints: (stack trace will vary based on where trace is called)
// Trace: Show me
// at repl:2:9
// at REPLServer.defaultEval (repl.js:248:27)
// at bound (domain.js:287:14)
// at REPLServer.runBound [as eval] (domain.js:300:12)
// at REPLServer.<anonymous> (repl.js:412:12)
// at emitOne (events.js:82:20)
// at REPLServer.emit (events.js:169:7)
// at REPLServer.Interface._onLine (readline.js:210:10)
// at REPLServer.Interface._line (readline.js:549:8)
// at REPLServer.Interface._ttyWrite (readline.js:826:14)
</code></pre>
<h3>console.warn([data][, ...])<span><a class="mark" href="#console_console_warn_data" id="console_console_warn_data">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>The <code>console.warn()</code> function is an alias for <a href="#console_console_error_data"><code>console.error()</code></a>.</p>
</div>
</div>
</div>
<script src="assets/sh_main.js"></script>
<script src="assets/sh_javascript.min.js"></script>
<script>highlight(undefined, undefined, 'pre');</script>
<!-- __TRACKING__ -->
</body>
</html>
|