Skip to content

Latest commit

Β 

History

History
335 lines (274 loc) Β· 10.2 KB

File metadata and controls

335 lines (274 loc) Β· 10.2 KB
Β 
Apr 11, 2015
Apr 11, 2015
1
# Console
Mar 1, 2012
Mar 1, 2012
2
Aug 4, 2016
Aug 4, 2016
3
> Stability: 2 - Stable
Mar 4, 2012
Mar 4, 2012
4
Dec 30, 2015
Dec 30, 2015
5
The `console` module provides a simple debugging console that is similar to the
6
JavaScript console mechanism provided by web browsers.
Apr 11, 2015
Apr 11, 2015
7
Dec 30, 2015
Dec 30, 2015
8
The module exports two specific components:
Apr 11, 2015
Apr 11, 2015
9
Dec 30, 2015
Dec 30, 2015
10
* A `Console` class with methods such as `console.log()`, `console.error()` and
11
`console.warn()` that can be used to write to any Node.js stream.
Feb 16, 2017
Feb 16, 2017
12
* A global `console` instance configured to write to [`process.stdout`][] and
13
[`process.stderr`][]. The global `console` can be used without calling
Dec 30, 2015
Dec 30, 2015
14
`require('console')`.
15
Feb 16, 2017
Feb 16, 2017
16
***Warning***: The global console object's methods are neither consistently
17
synchronous like the browser APIs they resemble, nor are they consistently
18
asynchronous like all other Node.js streams. See the [note on process I/O][] for
19
more information.
20
Dec 30, 2015
Dec 30, 2015
21
Example using the global `console`:
22
Jan 21, 2016
Jan 21, 2016
23
```js
24
console.log('hello world');
Nov 16, 2016
Nov 16, 2016
25
// Prints: hello world, to stdout
Jan 21, 2016
Jan 21, 2016
26
console.log('hello %s', 'world');
Nov 16, 2016
Nov 16, 2016
27
// Prints: hello world, to stdout
Jan 21, 2016
Jan 21, 2016
28
console.error(new Error('Whoops, something bad happened'));
Nov 16, 2016
Nov 16, 2016
29
// Prints: [Error: Whoops, something bad happened], to stderr
Jan 21, 2016
Jan 21, 2016
30
31
const name = 'Will Robinson';
32
console.warn(`Danger ${name}! Danger!`);
Nov 16, 2016
Nov 16, 2016
33
// Prints: Danger Will Robinson! Danger!, to stderr
Jan 21, 2016
Jan 21, 2016
34
```
Dec 30, 2015
Dec 30, 2015
35
36
Example using the `Console` class:
37
Jan 21, 2016
Jan 21, 2016
38
```js
39
const out = getStreamSomehow();
40
const err = getStreamSomehow();
41
const myConsole = new console.Console(out, err);
Dec 30, 2015
Dec 30, 2015
42
Jan 21, 2016
Jan 21, 2016
43
myConsole.log('hello world');
Nov 16, 2016
Nov 16, 2016
44
// Prints: hello world, to out
Jan 21, 2016
Jan 21, 2016
45
myConsole.log('hello %s', 'world');
Nov 16, 2016
Nov 16, 2016
46
// Prints: hello world, to out
Jan 21, 2016
Jan 21, 2016
47
myConsole.error(new Error('Whoops, something bad happened'));
Nov 16, 2016
Nov 16, 2016
48
// Prints: [Error: Whoops, something bad happened], to err
Dec 30, 2015
Dec 30, 2015
49
Jan 21, 2016
Jan 21, 2016
50
const name = 'Will Robinson';
51
myConsole.warn(`Danger ${name}! Danger!`);
Nov 16, 2016
Nov 16, 2016
52
// Prints: Danger Will Robinson! Danger!, to err
Jan 21, 2016
Jan 21, 2016
53
```
Dec 30, 2015
Dec 30, 2015
54
Nov 13, 2015
Nov 13, 2015
55
## Class: Console
Feb 24, 2017
Feb 24, 2017
56
<!-- YAML
57
changes:
May 30, 2017
May 30, 2017
58
- version: v8.0.0
Feb 24, 2017
Feb 24, 2017
59
pr-url: https://github.com/nodejs/node/pull/9744
60
description: Errors that occur while writing to the underlying streams
61
will now be ignored.
62
-->
Nov 13, 2015
Nov 13, 2015
63
64
<!--type=class-->
65
Dec 30, 2015
Dec 30, 2015
66
The `Console` class can be used to create a simple logger with configurable
67
output streams and can be accessed using either `require('console').Console`
Jun 2, 2017
Jun 2, 2017
68
or `console.Console` (or their destructured counterparts):
Nov 13, 2015
Nov 13, 2015
69
Jan 21, 2016
Jan 21, 2016
70
```js
Jun 2, 2017
Jun 2, 2017
71
const { Console } = require('console');
Apr 24, 2017
Apr 24, 2017
72
```
73
74
```js
Jun 2, 2017
Jun 2, 2017
75
const { Console } = console;
Jan 21, 2016
Jan 21, 2016
76
```
Nov 13, 2015
Nov 13, 2015
77
78
### new Console(stdout[, stderr])
Mar 1, 2017
Mar 1, 2017
79
* `stdout` {Writable}
80
* `stderr` {Writable}
Nov 13, 2015
Nov 13, 2015
81
Dec 30, 2015
Dec 30, 2015
82
Creates a new `Console` by passing one or two writable stream instances.
Nov 13, 2015
Nov 13, 2015
83
`stdout` is a writable stream to print log or info output. `stderr`
Jan 27, 2017
Jan 27, 2017
84
is used for warning or error output. If `stderr` is not passed, warning and error
Mar 16, 2016
Mar 16, 2016
85
output will be sent to `stdout`.
Nov 13, 2015
Nov 13, 2015
86
Jan 21, 2016
Jan 21, 2016
87
```js
88
const output = fs.createWriteStream('./stdout.log');
89
const errorOutput = fs.createWriteStream('./stderr.log');
90
// custom simple logger
91
const logger = new Console(output, errorOutput);
92
// use it like console
Dec 27, 2016
Dec 27, 2016
93
const count = 5;
Jan 21, 2016
Jan 21, 2016
94
logger.log('count: %d', count);
95
// in stdout.log: count 5
96
```
Nov 13, 2015
Nov 13, 2015
97
98
The global `console` is a special `Console` whose output is sent to
Feb 17, 2016
Feb 17, 2016
99
[`process.stdout`][] and [`process.stderr`][]. It is equivalent to calling:
Nov 13, 2015
Nov 13, 2015
100
Jan 21, 2016
Jan 21, 2016
101
```js
102
new Console(process.stdout, process.stderr);
103
```
Nov 13, 2015
Nov 13, 2015
104
Sep 14, 2016
Sep 14, 2016
105
### console.assert(value[, message][, ...args])
May 28, 2016
May 28, 2016
106
<!-- YAML
107
added: v0.1.101
108
-->
Mar 1, 2017
Mar 1, 2017
109
* `value` {any}
110
* `message` {any}
111
* `...args` {any}
Mar 23, 2013
Mar 23, 2013
112
Dec 30, 2015
Dec 30, 2015
113
A simple assertion test that verifies whether `value` is truthy. If it is not,
Feb 17, 2016
Feb 17, 2016
114
an `AssertionError` is thrown. If provided, the error `message` is formatted
Dec 30, 2015
Dec 30, 2015
115
using [`util.format()`][] and used as the error message.
116
Jan 21, 2016
Jan 21, 2016
117
```js
118
console.assert(true, 'does nothing');
Nov 16, 2016
Nov 16, 2016
119
// OK
Jan 21, 2016
Jan 21, 2016
120
console.assert(false, 'Whoops %s', 'didn\'t work');
Nov 16, 2016
Nov 16, 2016
121
// AssertionError: Whoops didn't work
Jan 21, 2016
Jan 21, 2016
122
```
May 1, 2012
May 1, 2012
123
May 25, 2017
May 25, 2017
124
*Note*: The `console.assert()` method is implemented differently in Node.js
125
than the `console.assert()` method [available in browsers][web-api-assert].
Apr 18, 2016
Apr 18, 2016
126
127
Specifically, in browsers, calling `console.assert()` with a falsy
128
assertion will cause the `message` to be printed to the console without
129
interrupting execution of subsequent code. In Node.js, however, a falsy
130
assertion will cause an `AssertionError` to be thrown.
131
132
Functionality approximating that implemented by browsers can be implemented
133
by extending Node.js' `console` and overriding the `console.assert()` method.
134
135
In the following example, a simple module is created that extends and overrides
136
the default behavior of `console` in Node.js.
137
Apr 24, 2017
Apr 24, 2017
138
<!-- eslint-disable func-name-matching -->
Apr 18, 2016
Apr 18, 2016
139
```js
140
'use strict';
141
142
// Creates a simple extension of console with a
143
// new impl for assert without monkey-patching.
Dec 27, 2016
Dec 27, 2016
144
const myConsole = Object.create(console, {
145
assert: {
Apr 24, 2017
Apr 24, 2017
146
value: function assert(assertion, message, ...args) {
Dec 27, 2016
Dec 27, 2016
147
try {
148
console.assert(assertion, message, ...args);
149
} catch (err) {
150
console.error(err.stack);
151
}
152
},
153
configurable: true,
154
enumerable: true,
155
writable: true,
156
},
157
});
Apr 18, 2016
Apr 18, 2016
158
159
module.exports = myConsole;
160
```
161
162
This can then be used as a direct replacement for the built in console:
163
164
```js
165
const console = require('./myConsole');
166
console.assert(false, 'this message will print, but no error thrown');
167
console.log('this will also print');
168
```
169
Apr 11, 2015
Apr 11, 2015
170
### console.dir(obj[, options])
May 28, 2016
May 28, 2016
171
<!-- YAML
172
added: v0.1.101
173
-->
Mar 1, 2017
Mar 1, 2017
174
* `obj` {any}
175
* `options` {Object}
Mar 8, 2017
Mar 8, 2017
176
* `showHidden` {boolean}
177
* `depth` {number}
178
* `colors` {boolean}
Apr 18, 2011
Apr 18, 2011
179
Feb 17, 2016
Feb 17, 2016
180
Uses [`util.inspect()`][] on `obj` and prints the resulting string to `stdout`.
Dec 30, 2015
Dec 30, 2015
181
This function bypasses any custom `inspect()` function defined on `obj`. An
Feb 17, 2016
Feb 17, 2016
182
optional `options` object may be passed to alter certain aspects of the
Dec 30, 2015
Dec 30, 2015
183
formatted string:
Jun 12, 2014
Jun 12, 2014
184
Jan 8, 2015
Jan 8, 2015
185
- `showHidden` - if `true` then the object's non-enumerable and symbol
186
properties will be shown too. Defaults to `false`.
Jun 12, 2014
Jun 12, 2014
187
Feb 17, 2016
Feb 17, 2016
188
- `depth` - tells [`util.inspect()`][] how many times to recurse while
189
formatting the object. This is useful for inspecting large complicated objects.
190
Defaults to `2`. To make it recurse indefinitely, pass `null`.
Jun 12, 2014
Jun 12, 2014
191
192
- `colors` - if `true`, then the output will be styled with ANSI color codes.
Dec 12, 2015
Dec 12, 2015
193
Defaults to `false`. Colors are customizable; see
194
[customizing `util.inspect()` colors][].
Apr 18, 2011
Apr 18, 2011
195
Sep 14, 2016
Sep 14, 2016
196
### console.error([data][, ...args])
May 28, 2016
May 28, 2016
197
<!-- YAML
198
added: v0.1.100
199
-->
Mar 1, 2017
Mar 1, 2017
200
* `data` {any}
201
* `...args` {any}
Nov 13, 2015
Nov 13, 2015
202
Feb 17, 2016
Feb 17, 2016
203
Prints to `stderr` with newline. Multiple arguments can be passed, with the
204
first used as the primary message and all additional used as substitution
Nov 19, 2016
Nov 19, 2016
205
values similar to printf(3) (the arguments are all passed to
Dec 30, 2015
Dec 30, 2015
206
[`util.format()`][]).
207
Jan 21, 2016
Jan 21, 2016
208
```js
209
const code = 5;
210
console.error('error #%d', code);
Nov 16, 2016
Nov 16, 2016
211
// Prints: error #5, to stderr
Jan 21, 2016
Jan 21, 2016
212
console.error('error', code);
Nov 16, 2016
Nov 16, 2016
213
// Prints: error 5, to stderr
Jan 21, 2016
Jan 21, 2016
214
```
Dec 30, 2015
Dec 30, 2015
215
216
If formatting elements (e.g. `%d`) are not found in the first string then
217
[`util.inspect()`][] is called on each argument and the resulting string
Feb 17, 2016
Feb 17, 2016
218
values are concatenated. See [`util.format()`][] for more information.
Nov 13, 2015
Nov 13, 2015
219
Sep 14, 2016
Sep 14, 2016
220
### console.info([data][, ...args])
May 28, 2016
May 28, 2016
221
<!-- YAML
222
added: v0.1.100
223
-->
Mar 1, 2017
Mar 1, 2017
224
* `data` {any}
225
* `...args` {any}
Nov 13, 2015
Nov 13, 2015
226
Dec 30, 2015
Dec 30, 2015
227
The `console.info()` function is an alias for [`console.log()`][].
Nov 13, 2015
Nov 13, 2015
228
Sep 14, 2016
Sep 14, 2016
229
### console.log([data][, ...args])
May 28, 2016
May 28, 2016
230
<!-- YAML
231
added: v0.1.100
232
-->
Mar 1, 2017
Mar 1, 2017
233
* `data` {any}
234
* `...args` {any}
Nov 13, 2015
Nov 13, 2015
235
Feb 17, 2016
Feb 17, 2016
236
Prints to `stdout` with newline. Multiple arguments can be passed, with the
237
first used as the primary message and all additional used as substitution
Nov 19, 2016
Nov 19, 2016
238
values similar to printf(3) (the arguments are all passed to
Dec 30, 2015
Dec 30, 2015
239
[`util.format()`][]).
Nov 13, 2015
Nov 13, 2015
240
Jan 21, 2016
Jan 21, 2016
241
```js
Dec 27, 2016
Dec 27, 2016
242
const count = 5;
Jan 21, 2016
Jan 21, 2016
243
console.log('count: %d', count);
Nov 16, 2016
Nov 16, 2016
244
// Prints: count: 5, to stdout
Jul 29, 2016
Jul 29, 2016
245
console.log('count:', count);
Nov 16, 2016
Nov 16, 2016
246
// Prints: count: 5, to stdout
Jan 21, 2016
Jan 21, 2016
247
```
Nov 13, 2015
Nov 13, 2015
248
Dec 30, 2015
Dec 30, 2015
249
If formatting elements (e.g. `%d`) are not found in the first string then
250
[`util.inspect()`][] is called on each argument and the resulting string
Feb 17, 2016
Feb 17, 2016
251
values are concatenated. See [`util.format()`][] for more information.
Nov 13, 2015
Nov 13, 2015
252
Nov 10, 2015
Nov 10, 2015
253
### console.time(label)
May 28, 2016
May 28, 2016
254
<!-- YAML
255
added: v0.1.104
256
-->
Mar 8, 2017
Mar 8, 2017
257
* `label` {string}
Apr 18, 2011
Apr 18, 2011
258
Oct 16, 2015
Oct 16, 2015
259
Starts a timer that can be used to compute the duration of an operation. Timers
Apr 28, 2017
Apr 28, 2017
260
are identified by a unique `label`. Use the same `label` when calling
Nov 16, 2015
Nov 16, 2015
261
[`console.timeEnd()`][] to stop the timer and output the elapsed time in
Jun 6, 2016
Jun 6, 2016
262
milliseconds to `stdout`. Timer durations are accurate to the sub-millisecond.
Apr 18, 2011
Apr 18, 2011
263
Nov 10, 2015
Nov 10, 2015
264
### console.timeEnd(label)
May 28, 2016
May 28, 2016
265
<!-- YAML
266
added: v0.1.104
Feb 24, 2017
Feb 24, 2017
267
changes:
268
- version: v6.0.0
269
pr-url: https://github.com/nodejs/node/pull/5901
270
description: This method no longer supports multiple calls that don’t map
271
to individual `console.time()` calls; see below for details.
May 28, 2016
May 28, 2016
272
-->
Mar 8, 2017
Mar 8, 2017
273
* `label` {string}
Apr 18, 2011
Apr 18, 2011
274
Dec 12, 2015
Dec 12, 2015
275
Stops a timer that was previously started by calling [`console.time()`][] and
Jun 6, 2016
Jun 6, 2016
276
prints the result to `stdout`:
Apr 18, 2011
Apr 18, 2011
277
Jan 21, 2016
Jan 21, 2016
278
```js
279
console.time('100-elements');
Apr 24, 2017
Apr 24, 2017
280
for (let i = 0; i < 100; i++) {}
Jan 21, 2016
Jan 21, 2016
281
console.timeEnd('100-elements');
282
// prints 100-elements: 225.438ms
283
```
Apr 18, 2011
Apr 18, 2011
284
May 25, 2017
May 25, 2017
285
*Note*: As of Node.js v6.0.0, `console.timeEnd()` deletes the timer to avoid
Apr 30, 2016
Apr 30, 2016
286
leaking it. On older versions, the timer persisted. This allowed
287
`console.timeEnd()` to be called multiple times for the same label. This
May 25, 2017
May 25, 2017
288
functionality was unintended and is no longer supported.
Apr 30, 2016
Apr 30, 2016
289
Mar 1, 2017
Mar 1, 2017
290
### console.trace([message][, ...args])
May 28, 2016
May 28, 2016
291
<!-- YAML
292
added: v0.1.104
293
-->
Mar 1, 2017
Mar 1, 2017
294
* `message` {any}
295
* `...args` {any}
Apr 18, 2011
Apr 18, 2011
296
Feb 17, 2016
Feb 17, 2016
297
Prints to `stderr` the string `'Trace :'`, followed by the [`util.format()`][]
Dec 30, 2015
Dec 30, 2015
298
formatted message and stack trace to the current position in the code.
299
Jan 21, 2016
Jan 21, 2016
300
```js
301
console.trace('Show me');
Nov 16, 2016
Nov 16, 2016
302
// Prints: (stack trace will vary based on where trace is called)
303
// Trace: Show me
304
// at repl:2:9
305
// at REPLServer.defaultEval (repl.js:248:27)
306
// at bound (domain.js:287:14)
307
// at REPLServer.runBound [as eval] (domain.js:300:12)
308
// at REPLServer.<anonymous> (repl.js:412:12)
309
// at emitOne (events.js:82:20)
310
// at REPLServer.emit (events.js:169:7)
311
// at REPLServer.Interface._onLine (readline.js:210:10)
312
// at REPLServer.Interface._line (readline.js:549:8)
313
// at REPLServer.Interface._ttyWrite (readline.js:826:14)
Jan 21, 2016
Jan 21, 2016
314
```
Apr 18, 2011
Apr 18, 2011
315
Sep 14, 2016
Sep 14, 2016
316
### console.warn([data][, ...args])
May 28, 2016
May 28, 2016
317
<!-- YAML
318
added: v0.1.100
319
-->
Mar 1, 2017
Mar 1, 2017
320
* `data` {any}
321
* `...args` {any}
Apr 11, 2015
Apr 11, 2015
322
Dec 30, 2015
Dec 30, 2015
323
The `console.warn()` function is an alias for [`console.error()`][].
Nov 16, 2015
Nov 16, 2015
324
Sep 14, 2016
Sep 14, 2016
325
[`console.error()`]: #console_console_error_data_args
326
[`console.log()`]: #console_console_log_data_args
Nov 16, 2015
Nov 16, 2015
327
[`console.time()`]: #console_console_time_label
Dec 3, 2015
Dec 3, 2015
328
[`console.timeEnd()`]: #console_console_timeend_label
Feb 17, 2016
Feb 17, 2016
329
[`process.stderr`]: process.html#process_process_stderr
330
[`process.stdout`]: process.html#process_process_stdout
Sep 14, 2016
Sep 14, 2016
331
[`util.format()`]: util.html#util_util_format_format_args
Dec 3, 2015
Dec 3, 2015
332
[`util.inspect()`]: util.html#util_util_inspect_object_options
Feb 17, 2016
Feb 17, 2016
333
[customizing `util.inspect()` colors]: util.html#util_customizing_util_inspect_colors
Feb 16, 2017
Feb 16, 2017
334
[note on process I/O]: process.html#process_a_note_on_process_i_o
Apr 18, 2016
Apr 18, 2016
335
[web-api-assert]: https://developer.mozilla.org/en-US/docs/Web/API/console/assert