1- # console
1+ # Console
22
33 Stability: 2 - Stable
44
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+ ## console
14+
515* {Object}
616
717<!-- type=global-->
818
9- For printing to stdout and stderr. Similar to the console object functions
19+ For printing to stdout and stderr. Similar to the console object functions
1020provided by most web browsers, here the output is sent to stdout or stderr.
1121
1222The console functions are synchronous when the destination is a terminal or
@@ -22,7 +32,7 @@ In daily use, the blocking/non-blocking dichotomy is not something you
2232should worry about unless you log huge amounts of data.
2333
2434
25- ## console.log([ data] [ , ... ] )
35+ ### console.log([ data] [ , ... ] )
2636
2737Prints to stdout with newline. This function can take multiple arguments in a
2838` printf() ` -like way. Example:
@@ -34,19 +44,19 @@ Prints to stdout with newline. This function can take multiple arguments in a
3444If formatting elements are not found in the first string then ` util.inspect `
3545is used on each argument. See [ util.format()] [ ] for more information.
3646
37- ## console.info([ data] [ , ... ] )
47+ ### console.info([ data] [ , ... ] )
3848
3949Same as ` console.log ` .
4050
41- ## console.error([ data] [ , ... ] )
51+ ### console.error([ data] [ , ... ] )
4252
4353Same as ` console.log ` but prints to stderr.
4454
45- ## console.warn([ data] [ , ... ] )
55+ ### console.warn([ data] [ , ... ] )
4656
4757Same as ` console.error ` .
4858
49- ## console.dir(obj[ , options] )
59+ ### console.dir(obj[ , options] )
5060
5161Uses ` util.inspect ` on ` obj ` and prints resulting string to stdout. This function
5262bypasses any custom ` inspect() ` function on ` obj ` . An optional * options* object
@@ -62,15 +72,15 @@ object. This is useful for inspecting large complicated objects. Defaults to
6272- ` colors ` - if ` true ` , then the output will be styled with ANSI color codes.
6373Defaults to ` false ` . Colors are customizable, see below.
6474
65- ## console.time(label)
75+ ### console.time(label)
6676
6777Used to calculate the duration of a specific operation. To start a timer, call
6878the ` console.time() ` method, giving it a name as only parameter. To stop the
6979timer, and to get the elapsed time in milliseconds, just call the
7080[ ` console.timeEnd() ` ] ( #console_console_timeend_label ) method, again passing the
7181timer's name as the parameter.
7282
73- ## console.timeEnd(label)
83+ ### console.timeEnd(label)
7484
7585Stops a timer that was previously started by calling
7686[ ` console.time() ` ] ( #console_console_time_label ) and print the result to the
@@ -85,15 +95,48 @@ Example:
8595 console.timeEnd('100-elements');
8696 // prints 100-elements: 262ms
8797
88- ## console.trace(message[ , ...] )
98+ ### console.trace(message[ , ...] )
8999
90100Print to stderr ` 'Trace :' ` , followed by the formatted message and stack trace
91101to the current position.
92102
93- ## console.assert(value[ , message] [ , ... ] )
103+ ### console.assert(value[ , message] [ , ... ] )
94104
95105Similar to [ assert.ok()] [ ] , but the error message is formatted as
96106` util.format(message...) ` .
97107
108+ ## Class: Console
109+
110+ <!-- type=class-->
111+
112+ Use ` require('console').Console ` or ` console.Console ` to access this class.
113+
114+ var Console = require('console').Console;
115+ var Console = console.Console;
116+
117+ You can use ` Console ` class to custom simple logger like ` console ` , but with
118+ different output streams.
119+
120+ ### new Console(stdout[ , stderr] )
121+
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 ` .
126+
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
132+ var count = 5;
133+ logger.log('count: %d', count);
134+ // in stdout.log: count 5
135+
136+ The global ` console ` is a special ` Console ` whose output is sent to
137+ ` process.stdout ` and ` process.stderr ` :
138+
139+ new Console(process.stdout, process.stderr);
140+
98141[ assert.ok() ] : assert.html#assert_assert_value_message_assert_ok_value_message
99142[ util.format() ] : util.html#util_util_format_format
0 commit comments