1
- # console
1
+ # Console
2
2
3
3
Stability: 2 - Stable
4
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
+ ## console
14
+
5
15
* {Object}
6
16
7
17
<!-- type=global-->
8
18
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
10
20
provided by most web browsers, here the output is sent to stdout or stderr.
11
21
12
22
The 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
22
32
should worry about unless you log huge amounts of data.
23
33
24
34
25
- ## console.log([ data] [ , ... ] )
35
+ ### console.log([ data] [ , ... ] )
26
36
27
37
Prints to stdout with newline. This function can take multiple arguments in a
28
38
` printf() ` -like way. Example:
@@ -34,19 +44,19 @@ Prints to stdout with newline. This function can take multiple arguments in a
34
44
If formatting elements are not found in the first string then ` util.inspect `
35
45
is used on each argument. See [ util.format()] [ ] for more information.
36
46
37
- ## console.info([ data] [ , ... ] )
47
+ ### console.info([ data] [ , ... ] )
38
48
39
49
Same as ` console.log ` .
40
50
41
- ## console.error([ data] [ , ... ] )
51
+ ### console.error([ data] [ , ... ] )
42
52
43
53
Same as ` console.log ` but prints to stderr.
44
54
45
- ## console.warn([ data] [ , ... ] )
55
+ ### console.warn([ data] [ , ... ] )
46
56
47
57
Same as ` console.error ` .
48
58
49
- ## console.dir(obj[ , options] )
59
+ ### console.dir(obj[ , options] )
50
60
51
61
Uses ` util.inspect ` on ` obj ` and prints resulting string to stdout. This function
52
62
bypasses 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
62
72
- ` colors ` - if ` true ` , then the output will be styled with ANSI color codes.
63
73
Defaults to ` false ` . Colors are customizable, see below.
64
74
65
- ## console.time(label)
75
+ ### console.time(label)
66
76
67
77
Used to calculate the duration of a specific operation. To start a timer, call
68
78
the ` console.time() ` method, giving it a name as only parameter. To stop the
69
79
timer, and to get the elapsed time in milliseconds, just call the
70
80
[ ` console.timeEnd() ` ] ( #console_console_timeend_label ) method, again passing the
71
81
timer's name as the parameter.
72
82
73
- ## console.timeEnd(label)
83
+ ### console.timeEnd(label)
74
84
75
85
Stops a timer that was previously started by calling
76
86
[ ` console.time() ` ] ( #console_console_time_label ) and print the result to the
@@ -85,15 +95,48 @@ Example:
85
95
console.timeEnd('100-elements');
86
96
// prints 100-elements: 262ms
87
97
88
- ## console.trace(message[ , ...] )
98
+ ### console.trace(message[ , ...] )
89
99
90
100
Print to stderr ` 'Trace :' ` , followed by the formatted message and stack trace
91
101
to the current position.
92
102
93
- ## console.assert(value[ , message] [ , ... ] )
103
+ ### console.assert(value[ , message] [ , ... ] )
94
104
95
105
Similar to [ assert.ok()] [ ] , but the error message is formatted as
96
106
` util.format(message...) ` .
97
107
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
+
98
141
[ assert.ok() ] : assert.html#assert_assert_value_message_assert_ok_value_message
99
142
[ util.format() ] : util.html#util_util_format_format
0 commit comments