doc: add links and backticks around names
[platform/upstream/nodejs.git] / doc / api / console.markdown
1 # Console
2
3     Stability: 2 - Stable
4
5 The module defines a `Console` class and exports a `console` object.
6
7 The `console` object is a special instance of `Console` whose output is
8 sent to stdout or stderr.
9
10 For ease of use, `console` is defined as a global object and can be used
11 directly without `require`.
12
13 ## Class: Console
14
15 <!--type=class-->
16
17 Use `require('console').Console` or `console.Console` to access this class.
18
19     var Console = require('console').Console;
20     var Console = console.Console;
21
22 You can use `Console` class to custom simple logger like `console`, but with
23 different output streams.
24
25 ### new Console(stdout[, stderr])
26
27 Create a new `Console` by passing one or two writable stream instances.
28 `stdout` is a writable stream to print log or info output. `stderr`
29 is used for warning or error output. If `stderr` isn't passed, the warning
30 and error output will be sent to the `stdout`.
31
32     var output = fs.createWriteStream('./stdout.log');
33     var errorOutput = fs.createWriteStream('./stderr.log');
34     // custom simple logger
35     var logger = new Console(output, errorOutput);
36     // use it like console
37     var count = 5;
38     logger.log('count: %d', count);
39     // in stdout.log: count 5
40
41 The global `console` is a special `Console` whose output is sent to
42 `process.stdout` and `process.stderr`:
43
44     new Console(process.stdout, process.stderr);
45
46 ## console
47
48 * {Object}
49
50 <!--type=global-->
51
52 For printing to stdout and stderr. Similar to the console object functions
53 provided by most web browsers, here the output is sent to stdout or stderr.
54
55 The console functions are synchronous when the destination is a terminal or
56 a file (to avoid lost messages in case of premature exit) and asynchronous
57 when it's a pipe (to avoid blocking for long periods of time).
58
59 That is, in the following example, stdout is non-blocking while stderr
60 is blocking:
61
62     $ node script.js 2> error.log | tee info.log
63
64 In daily use, the blocking/non-blocking dichotomy is not something you
65 should worry about unless you log huge amounts of data.
66
67 ### console.assert(value[, message][, ...])
68
69 Similar to [`assert.ok()`][], but the error message is formatted as
70 `util.format(message...)`.
71
72 ### console.dir(obj[, options])
73
74 Uses [`util.inspect()`][] on `obj` and prints resulting string to stdout. This function
75 bypasses any custom `inspect()` function on `obj`. An optional *options* object
76 may be passed that alters certain aspects of the formatted string:
77
78 - `showHidden` - if `true` then the object's non-enumerable and symbol
79 properties will be shown too. Defaults to `false`.
80
81 - `depth` - tells `inspect` how many times to recurse while formatting the
82 object. This is useful for inspecting large complicated objects. Defaults to
83 `2`. To make it recurse indefinitely pass `null`.
84
85 - `colors` - if `true`, then the output will be styled with ANSI color codes.
86 Defaults to `false`. Colors are customizable, see [customizing `util.inspect()` colors][].
87
88 ### console.error([data][, ...])
89
90 Same as [`console.log()`][] but prints to stderr.
91
92 ### console.info([data][, ...])
93
94 Same as [`console.log()`][].
95
96 ### console.log([data][, ...])
97
98 Prints to stdout with newline. This function can take multiple arguments in a
99 `printf()`-like way. Example:
100
101     var count = 5;
102     console.log('count: %d', count);
103     // prints 'count: 5'
104
105 If formatting elements are not found in the first string then [`util.inspect()`][]
106 is used on each argument.  See [`util.format()`][] for more information.
107
108 ### console.time(label)
109
110 Used to calculate the duration of a specific operation. To start a timer, call
111 the `console.time()` method, giving it a name as only parameter. To stop the
112 timer, and to get the elapsed time in milliseconds, just call the
113 [`console.timeEnd()`][] method, again passing the
114 timer's name as the parameter.
115
116 ### console.timeEnd(label)
117
118 Stops a timer that was previously started by calling
119 [`console.time()`][] and prints the result to the
120 console.
121
122 Example:
123
124     console.time('100-elements');
125     for (var i = 0; i < 100; i++) {
126       ;
127     }
128     console.timeEnd('100-elements');
129     // prints 100-elements: 262ms
130
131 ### console.trace(message[, ...])
132
133 Print to stderr `'Trace :'`, followed by the formatted message and stack trace
134 to the current position.
135
136 ### console.warn([data][, ...])
137
138 Same as [`console.error()`][].
139
140 [`assert.ok()`]: assert.html#assert_assert_value_message_assert_ok_value_message
141 [`console.error()`]: #console_console_error_data
142 [`console.log()`]: #console_console_log_data
143 [`console.time()`]: #console_console_time_label
144 [`console.timeEnd()`]: #console_console_timeend_label
145 [`util.format()`]: util.html#util_util_format_format
146 [`util.inspect()`]: util.html#util_util_inspect_object_options
147 [customizing `util.inspect()` colors]: util.html#util_customizing_util_inspect_colors