console.html 15.4 KB
Newer Older
1 2
<!doctype html>
<html lang="en">
3
<head>
4
  <meta charset="utf-8">
5
  <title>Console Node.js v5.5.0 Manual &amp; Documentation</title>
6
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
7 8
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
9
  <link rel="canonical" href="https://nodejs.org/api/console.html">
10
</head>
11 12 13 14 15 16 17 18
<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 (1)
        </a>
      </div>
19
      <ul>
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
<li><a class="nav-documentation" href="documentation.html">About these Docs</a></li>
<li><a class="nav-synopsis" href="synopsis.html">Synopsis</a></li>
<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-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>

60
    </div>
61 62 63

    <div id="column1" data-id="console" class="interior">
      <header>
64
        <h1>Node.js v5.5.0 Documentation</h1>
65 66 67 68 69 70 71 72 73 74 75 76 77 78
        <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><a href="#console_console">Console</a><ul>
79
<li><a href="#console_asynchronous_vs_synchronous_consoles">Asynchronous vs Synchronous Consoles</a></li>
80 81 82
<li><a href="#console_class_console">Class: Console</a><ul>
<li><a href="#console_new_console_stdout_stderr">new Console(stdout[, stderr])</a></li>
<li><a href="#console_console_assert_value_message">console.assert(value[, message][, ...])</a></li>
83
<li><a href="#console_console_dir_obj_options">console.dir(obj[, options])</a></li>
84 85 86 87 88
<li><a href="#console_console_error_data">console.error([data][, ...])</a></li>
<li><a href="#console_console_info_data">console.info([data][, ...])</a></li>
<li><a href="#console_console_log_data">console.log([data][, ...])</a></li>
<li><a href="#console_console_time_label">console.time(label)</a></li>
<li><a href="#console_console_timeend_label">console.timeEnd(label)</a></li>
89
<li><a href="#console_console_trace_message">console.trace(message[, ...])</a></li>
90
<li><a href="#console_console_warn_data">console.warn([data][, ...])</a></li>
91
</ul>
92 93 94 95 96 97
</li>
</ul>
</li>
</ul>

      </div>
98

99 100
      <div id="apicontent">
        <h1>Console<span><a class="mark" href="#console_console" id="console_console">#</a></span></h1>
101 102
<pre class="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.
103 104

</p>
105
<p>The module exports two specific components:
106

107
</p>
108 109 110 111 112 113 114 115
<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(&#39;console&#39;)</code>.</li>
</ul>
<p>Example using the global <code>console</code>:
116 117

</p>
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
<pre><code>console.log(&#39;hello world&#39;);
  // Prints: hello world, to stdout
console.log(&#39;hello %s&#39;, &#39;world&#39;);
  // Prints: hello world, to stdout
console.error(new Error(&#39;Whoops, something bad happened&#39;));
  // Prints: [Error: Whoops, something bad happened], to stderr

const name = &#39;Will Robinson&#39;;
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>const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
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
myConsole.log(&#39;hello world&#39;);
  // Prints: hello world, to out
myConsole.log(&#39;hello %s&#39;, &#39;world&#39;);
  // Prints: hello world, to out
myConsole.error(new Error(&#39;Whoops, something bad happened&#39;));
  // Prints: [Error: Whoops, something bad happened], to err

const name = &#39;Will Robinson&#39;;
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
Web browser <code>console</code> object, the <code>Console</code> is Node.js is <em>not</em> intended to
duplicate the browsers 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 synchronous when the destination is a terminal or
a file (to avoid lost messages in case of premature exit) and asynchronous
when the destination is a pipe (to avoid blocking for long periods of time).

</p>
<p>In the following example, stdout is non-blocking while stderr is blocking:

</p>
<pre><code>$ node script.js 2&gt; error.log | tee info.log</code></pre>
<p>Typically, the distinction between blocking/non-blocking is not important
unless an application is logging significant amounts of data. High volume
logging <em>should</em> use a <code>Console</code> instance that writes to a pipe.
163 164

</p>
165 166 167 168 169 170
<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(&#39;console&#39;).Console</code>
or <code>console.Console</code>:
171 172

</p>
173 174
<pre><code>const Console = require(&#39;console&#39;).Console;
const Console = console.Console;</code></pre>
175
<h3>new Console(stdout[, stderr])<span><a class="mark" href="#console_new_console_stdout_stderr" id="console_new_console_stdout_stderr">#</a></span></h3>
176
<p>Creates a new <code>Console</code> by passing one or two writable stream instances.
177 178 179 180 181
<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&#39;t passed, the warning
and error output will be sent to the <code>stdout</code>.

</p>
182 183
<pre><code>const output = fs.createWriteStream(&#39;./stdout.log&#39;);
const errorOutput = fs.createWriteStream(&#39;./stderr.log&#39;);
184
// custom simple logger
185
const logger = new Console(output, errorOutput);
186 187 188 189 190
// use it like console
var count = 5;
logger.log(&#39;count: %d&#39;, 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
191
<code>process.stdout</code> and <code>process.stderr</code>. It is equivalent to calling:
192 193 194 195

</p>
<pre><code>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>
196 197 198
<p>A simple assertion test that verifies whether <code>value</code> is truthy. If it is not,
an <code>AssertionError</code> is throw. 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.
199

200
</p>
201 202 203 204
<pre><code>console.assert(true, &#39;does nothing&#39;);
  // OK
console.assert(false, &#39;Whoops %s&#39;, &#39;didn\&#39;t work&#39;);
  // AssertionError: Whoops didn&#39;t work</code></pre>
205
<h3>console.dir(obj[, options])<span><a class="mark" href="#console_console_dir_obj_options" id="console_console_dir_obj_options">#</a></span></h3>
206
<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 stdout.
207 208 209
This function bypasses any custom <code>inspect()</code> function defined on <code>obj</code>. An
optional <code>options</code> object may be passed that alters certain aspects of the
formatted string:
210 211 212 213 214 215 216 217

</p>
<ul>
<li><p><code>showHidden</code> - if <code>true</code> then the object&#39;s non-enumerable and symbol
properties will be shown too. Defaults to <code>false</code>.</p>
</li>
<li><p><code>depth</code> - tells <code>inspect</code> how many times to recurse while formatting the
object. This is useful for inspecting large complicated objects. Defaults to
218
<code>2</code>. To make it recurse indefinitely, pass <code>null</code>.</p>
219 220
</li>
<li><p><code>colors</code> - if <code>true</code>, then the output will be styled with ANSI color codes.
221
Defaults to <code>false</code>. Colors are customizable; see
222
[customizing <code>util.inspect()</code> colors][].</p>
223 224
</li>
</ul>
225
<h3>console.error([data][, ...])<span><a class="mark" href="#console_console_error_data" id="console_console_error_data">#</a></span></h3>
226 227 228 229 230 231 232 233 234 235 236 237 238 239
<p>Prints to stderr with newline. Multiple arguments can be passed, with the first
used as the primary message and all additional used as substitution
values similar to <code>printf()</code> (the arguments are all passed to
<a href="util.html#util_util_format_format"><code>util.format()</code></a>).

</p>
<pre><code>const code = 5;
console.error(&#39;error #%d&#39;, code);
  // Prints: error #5, to stderr
console.error(&#39;error&#39;, 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.
240 241 242

</p>
<h3>console.info([data][, ...])<span><a class="mark" href="#console_console_info_data" id="console_console_info_data">#</a></span></h3>
243
<p>The <code>console.info()</code> function is an alias for <a href="#console_console_log_data"><code>console.log()</code></a>.
244 245 246

</p>
<h3>console.log([data][, ...])<span><a class="mark" href="#console_console_log_data" id="console_console_log_data">#</a></span></h3>
247 248 249 250
<p>Prints to stdout with newline. Multiple arguments can be passed, with the first
used as the primary message and all additional used as substitution
values similar to <code>printf()</code> (the arguments are all passed to
<a href="util.html#util_util_format_format"><code>util.format()</code></a>).
251 252 253 254

</p>
<pre><code>var count = 5;
console.log(&#39;count: %d&#39;, count);
255 256 257 258 259 260
  // Prints: count: 5, to stdout
console.log(&#39;count: &#39;, 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.
261 262 263

</p>
<h3>console.time(label)<span><a class="mark" href="#console_console_time_label" id="console_console_time_label">#</a></span></h3>
264
<p>Starts a timer that can be used to compute the duration of an operation. Timers
265
are identified by a unique <code>label</code>. Use the same <code>label</code> when you call
266
<a href="#console_console_timeend_label"><code>console.timeEnd()</code></a> to stop the timer and output the elapsed time in
267
milliseconds to stdout. Timer durations are accurate to the sub-millisecond.
268

269
</p>
270
<h3>console.timeEnd(label)<span><a class="mark" href="#console_console_timeend_label" id="console_console_timeend_label">#</a></span></h3>
271
<p>Stops a timer that was previously started by calling <a href="#console_console_time_label"><code>console.time()</code></a> and
272
prints the result to stdout:
273

274
</p>
275
<pre><code>console.time(&#39;100-elements&#39;);
276 277 278
for (var i = 0; i &lt; 100; i++) {
  ;
}
279
console.timeEnd(&#39;100-elements&#39;);
280
// prints 100-elements: 225.438ms</code></pre>
281
<h3>console.trace(message[, ...])<span><a class="mark" href="#console_console_trace_message" id="console_console_trace_message">#</a></span></h3>
282 283
<p>Prints to stderr the string <code>&#39;Trace :&#39;</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.
284

285
</p>
286 287 288 289 290 291 292 293 294 295 296 297 298
<pre><code>console.trace(&#39;Show me&#39;);
  // 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.&lt;anonymous&gt; (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>
299
<h3>console.warn([data][, ...])<span><a class="mark" href="#console_console_warn_data" id="console_console_warn_data">#</a></span></h3>
300
<p>The <code>console.warn()</code> function is an alias for <a href="#console_console_error_data"><code>console.error()</code></a>.
301 302

</p>
303

304
      </div>
305
    </div>
306 307 308
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
309
  <script>highlight(undefined, undefined, 'pre');</script>
310 311
</body>
</html>
312