doc: sort console alphabetically
[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 [assert.ok()]: assert.html#assert_assert_value_message_assert_ok_value_message
47 [util.format()]: util.html#util_util_format_format
48
49 ## console
50
51 * {Object}
52
53 <!--type=global-->
54
55 For printing to stdout and stderr. Similar to the console object functions
56 provided by most web browsers, here the output is sent to stdout or stderr.
57
58 The console functions are synchronous when the destination is a terminal or
59 a file (to avoid lost messages in case of premature exit) and asynchronous
60 when it's a pipe (to avoid blocking for long periods of time).
61
62 That is, in the following example, stdout is non-blocking while stderr
63 is blocking:
64
65     $ node script.js 2> error.log | tee info.log
66
67 In daily use, the blocking/non-blocking dichotomy is not something you
68 should worry about unless you log huge amounts of data.
69
70 ### console.assert(value[, message][, ...])
71
72 Similar to [assert.ok()][], but the error message is formatted as
73 `util.format(message...)`.
74
75 ### console.dir(obj[, options])
76
77 Uses `util.inspect` on `obj` and prints resulting string to stdout. This function
78 bypasses any custom `inspect()` function on `obj`. An optional *options* object
79 may be passed that alters certain aspects of the formatted string:
80
81 - `showHidden` - if `true` then the object's non-enumerable and symbol
82 properties will be shown too. Defaults to `false`.
83
84 - `depth` - tells `inspect` how many times to recurse while formatting the
85 object. This is useful for inspecting large complicated objects. Defaults to
86 `2`. To make it recurse indefinitely pass `null`.
87
88 - `colors` - if `true`, then the output will be styled with ANSI color codes.
89 Defaults to `false`. Colors are customizable, see below.
90
91 ### console.error([data][, ...])
92
93 Same as `console.log` but prints to stderr.
94
95 ### console.info([data][, ...])
96
97 Same as `console.log`.
98
99 ### console.log([data][, ...])
100
101 Prints to stdout with newline. This function can take multiple arguments in a
102 `printf()`-like way. Example:
103
104     var count = 5;
105     console.log('count: %d', count);
106     // prints 'count: 5'
107
108 If formatting elements are not found in the first string then `util.inspect`
109 is used on each argument.  See [util.format()][] for more information.
110
111 ### console.time(label)
112
113 Used to calculate the duration of a specific operation. To start a timer, call
114 the `console.time()` method, giving it a name as only parameter. To stop the
115 timer, and to get the elapsed time in milliseconds, just call the
116 [`console.timeEnd()`](#console_console_timeend_label) method, again passing the
117 timer's name as the parameter.
118
119 ### console.timeEnd(label)
120
121 Stops a timer that was previously started by calling
122 [`console.time()`](#console_console_time_label) and print the result to the
123 console.
124
125 Example:
126
127     console.time('100-elements');
128     for (var i = 0; i < 100; i++) {
129       ;
130     }
131     console.timeEnd('100-elements');
132     // prints 100-elements: 262ms
133
134 ### console.trace(message[, ...])
135
136 Print to stderr `'Trace :'`, followed by the formatted message and stack trace
137 to the current position.
138
139 ### console.warn([data][, ...])
140
141 Same as `console.error`.