5 The module defines a `Console` class and exports a `console` object.
7 The `console` object is a special instance of `Console` whose output is
8 sent to stdout or stderr.
10 For ease of use, `console` is defined as a global object and can be used
11 directly without `require`.
19 For printing to stdout and stderr. Similar to the console object functions
20 provided by most web browsers, here the output is sent to stdout or stderr.
22 The console functions are synchronous when the destination is a terminal or
23 a file (to avoid lost messages in case of premature exit) and asynchronous
24 when it's a pipe (to avoid blocking for long periods of time).
26 That is, in the following example, stdout is non-blocking while stderr
29 $ node script.js 2> error.log | tee info.log
31 In daily use, the blocking/non-blocking dichotomy is not something you
32 should worry about unless you log huge amounts of data.
35 ### console.log([data][, ...])
37 Prints to stdout with newline. This function can take multiple arguments in a
38 `printf()`-like way. Example:
41 console.log('count: %d', count);
44 If formatting elements are not found in the first string then `util.inspect`
45 is used on each argument. See [util.format()][] for more information.
47 ### console.info([data][, ...])
49 Same as `console.log`.
51 ### console.error([data][, ...])
53 Same as `console.log` but prints to stderr.
55 ### console.warn([data][, ...])
57 Same as `console.error`.
59 ### console.dir(obj[, options])
61 Uses `util.inspect` on `obj` and prints resulting string to stdout. This function
62 bypasses any custom `inspect()` function on `obj`. An optional *options* object
63 may be passed that alters certain aspects of the formatted string:
65 - `showHidden` - if `true` then the object's non-enumerable and symbol
66 properties will be shown too. Defaults to `false`.
68 - `depth` - tells `inspect` how many times to recurse while formatting the
69 object. This is useful for inspecting large complicated objects. Defaults to
70 `2`. To make it recurse indefinitely pass `null`.
72 - `colors` - if `true`, then the output will be styled with ANSI color codes.
73 Defaults to `false`. Colors are customizable, see below.
75 ### console.time(label)
77 Used to calculate the duration of a specific operation. To start a timer, call
78 the `console.time()` method, giving it a name as only parameter. To stop the
79 timer, and to get the elapsed time in milliseconds, just call the
80 [`console.timeEnd()`](#console_console_timeend_label) method, again passing the
81 timer's name as the parameter.
83 ### console.timeEnd(label)
85 Stops a timer that was previously started by calling
86 [`console.time()`](#console_console_time_label) and print the result to the
91 console.time('100-elements');
92 for (var i = 0; i < 100; i++) {
95 console.timeEnd('100-elements');
96 // prints 100-elements: 262ms
98 ### console.trace(message[, ...])
100 Print to stderr `'Trace :'`, followed by the formatted message and stack trace
101 to the current position.
103 ### console.assert(value[, message][, ...])
105 Similar to [assert.ok()][], but the error message is formatted as
106 `util.format(message...)`.
112 Use `require('console').Console` or `console.Console` to access this class.
114 var Console = require('console').Console;
115 var Console = console.Console;
117 You can use `Console` class to custom simple logger like `console`, but with
118 different output streams.
120 ### new Console(stdout[, stderr])
122 Create a new `Console` by passing one or two writable stream instances.
123 `stdout` is a writable stream to print log or info output. `stderr`
124 is used for warning or error output. If `stderr` isn't passed, the warning
125 and error output will be sent to the `stdout`.
127 var output = fs.createWriteStream('./stdout.log');
128 var errorOutput = fs.createWriteStream('./stderr.log');
129 // custom simple logger
130 var logger = new Console(output, errorOutput);
131 // use it like console
133 logger.log('count: %d', count);
134 // in stdout.log: count 5
136 The global `console` is a special `Console` whose output is sent to
137 `process.stdout` and `process.stderr`:
139 new Console(process.stdout, process.stderr);
141 [assert.ok()]: assert.html#assert_assert_value_message_assert_ok_value_message
142 [util.format()]: util.html#util_util_format_format