@@ -15,33 +15,37 @@ The module exports two specific components:
1515
1616Example using the global ` console ` :
1717
18- console.log('hello world');
19- // Prints: hello world, to stdout
20- console.log('hello %s', 'world');
21- // Prints: hello world, to stdout
22- console.error(new Error('Whoops, something bad happened'));
23- // Prints: [Error: Whoops, something bad happened], to stderr
24-
25- const name = 'Will Robinson';
26- console.warn(`Danger ${name}! Danger!`);
27- // Prints: Danger Will Robinson! Danger!, to stderr
18+ ``` js
19+ console .log (' hello world' );
20+ // Prints: hello world, to stdout
21+ console .log (' hello %s' , ' world' );
22+ // Prints: hello world, to stdout
23+ console .error (new Error (' Whoops, something bad happened' ));
24+ // Prints: [Error: Whoops, something bad happened], to stderr
25+
26+ const name = ' Will Robinson' ;
27+ console .warn (` Danger ${ name} ! Danger!` );
28+ // Prints: Danger Will Robinson! Danger!, to stderr
29+ ```
2830
2931Example using the ` Console ` class:
3032
31- const out = getStreamSomehow();
32- const err = getStreamSomehow();
33- const myConsole = new console.Console(out, err);
33+ ``` js
34+ const out = getStreamSomehow ();
35+ const err = getStreamSomehow ();
36+ const myConsole = new console.Console (out, err);
3437
35- myConsole.log('hello world');
36- // Prints: hello world, to out
37- myConsole.log('hello %s', 'world');
38- // Prints: hello world, to out
39- myConsole.error(new Error('Whoops, something bad happened'));
40- // Prints: [Error: Whoops, something bad happened], to err
38+ myConsole .log (' hello world' );
39+ // Prints: hello world, to out
40+ myConsole .log (' hello %s' , ' world' );
41+ // Prints: hello world, to out
42+ myConsole .error (new Error (' Whoops, something bad happened' ));
43+ // Prints: [Error: Whoops, something bad happened], to err
4144
42- const name = 'Will Robinson';
43- myConsole.warn(`Danger ${name}! Danger!`);
44- // Prints: Danger Will Robinson! Danger!, to err
45+ const name = ' Will Robinson' ;
46+ myConsole .warn (` Danger ${ name} ! Danger!` );
47+ // Prints: Danger Will Robinson! Danger!, to err
48+ ```
4549
4650While the API for the ` Console ` class is designed fundamentally around the
4751Web browser ` console ` object, the ` Console ` is Node.js is * not* intended to
@@ -55,7 +59,9 @@ when the destination is a pipe (to avoid blocking for long periods of time).
5559
5660In the following example, stdout is non-blocking while stderr is blocking:
5761
58- $ node script.js 2> error.log | tee info.log
62+ ```
63+ $ node script.js 2> error.log | tee info.log
64+ ```
5965
6066Typically, the distinction between blocking/non-blocking is not important
6167unless an application is logging significant amounts of data. High volume
@@ -69,8 +75,10 @@ The `Console` class can be used to create a simple logger with configurable
6975output streams and can be accessed using either ` require('console').Console `
7076or ` console.Console ` :
7177
72- const Console = require('console').Console;
73- const Console = console.Console;
78+ ``` js
79+ const Console = require (' console' ).Console ;
80+ const Console = console .Console ;
81+ ```
7482
7583### new Console(stdout[ , stderr] )
7684
@@ -79,30 +87,36 @@ Creates a new `Console` by passing one or two writable stream instances.
7987is used for warning or error output. If ` stderr ` isn't passed, the warning
8088and error output will be sent to the ` stdout ` .
8189
82- const output = fs.createWriteStream('./stdout.log');
83- const errorOutput = fs.createWriteStream('./stderr.log');
84- // custom simple logger
85- const logger = new Console(output, errorOutput);
86- // use it like console
87- var count = 5;
88- logger.log('count: %d', count);
89- // in stdout.log: count 5
90+ ``` js
91+ const output = fs .createWriteStream (' ./stdout.log' );
92+ const errorOutput = fs .createWriteStream (' ./stderr.log' );
93+ // custom simple logger
94+ const logger = new Console (output, errorOutput);
95+ // use it like console
96+ var count = 5 ;
97+ logger .log (' count: %d' , count);
98+ // in stdout.log: count 5
99+ ```
90100
91101The global ` console ` is a special ` Console ` whose output is sent to
92102` process.stdout ` and ` process.stderr ` . It is equivalent to calling:
93103
94- new Console(process.stdout, process.stderr);
104+ ``` js
105+ new Console (process .stdout , process .stderr );
106+ ```
95107
96108### console.assert(value[ , message] [ , ... ] )
97109
98110A simple assertion test that verifies whether ` value ` is truthy. If it is not,
99111an ` AssertionError ` is throw. If provided, the error ` message ` is formatted
100112using [ ` util.format() ` ] [ ] and used as the error message.
101113
102- console.assert(true, 'does nothing');
103- // OK
104- console.assert(false, 'Whoops %s', 'didn\'t work');
105- // AssertionError: Whoops didn't work
114+ ``` js
115+ console .assert (true , ' does nothing' );
116+ // OK
117+ console .assert (false , ' Whoops %s' , ' didn\' t work' );
118+ // AssertionError: Whoops didn't work
119+ ```
106120
107121### console.dir(obj[ , options] )
108122
@@ -129,11 +143,13 @@ used as the primary message and all additional used as substitution
129143values similar to ` printf() ` (the arguments are all passed to
130144[ ` util.format() ` ] [ ] ).
131145
132- const code = 5;
133- console.error('error #%d', code);
134- // Prints: error #5, to stderr
135- console.error('error', code);
136- // Prints: error 5, to stderr
146+ ``` js
147+ const code = 5 ;
148+ console .error (' error #%d' , code);
149+ // Prints: error #5, to stderr
150+ console .error (' error' , code);
151+ // Prints: error 5, to stderr
152+ ```
137153
138154If formatting elements (e.g. ` %d ` ) are not found in the first string then
139155[ ` util.inspect() ` ] [ ] is called on each argument and the resulting string
@@ -150,11 +166,13 @@ used as the primary message and all additional used as substitution
150166values similar to ` printf() ` (the arguments are all passed to
151167[ ` util.format() ` ] [ ] ).
152168
153- var count = 5;
154- console.log('count: %d', count);
155- // Prints: count: 5, to stdout
156- console.log('count: ', count);
157- // Prints: count: 5, to stdout
169+ ``` js
170+ var count = 5 ;
171+ console .log (' count: %d' , count);
172+ // Prints: count: 5, to stdout
173+ console .log (' count: ' , count);
174+ // Prints: count: 5, to stdout
175+ ```
158176
159177If formatting elements (e.g. ` %d ` ) are not found in the first string then
160178[ ` util.inspect() ` ] [ ] is called on each argument and the resulting string
@@ -172,31 +190,35 @@ milliseconds to stdout. Timer durations are accurate to the sub-millisecond.
172190Stops a timer that was previously started by calling [ ` console.time() ` ] [ ] and
173191prints the result to stdout:
174192
175- console.time('100-elements');
176- for (var i = 0; i < 100; i++) {
177- ;
178- }
179- console.timeEnd('100-elements');
180- // prints 100-elements: 225.438ms
193+ ``` js
194+ console .time (' 100-elements' );
195+ for (var i = 0 ; i < 100 ; i++ ) {
196+ ;
197+ }
198+ console .timeEnd (' 100-elements' );
199+ // prints 100-elements: 225.438ms
200+ ```
181201
182202### console.trace(message[ , ...] )
183203
184204Prints to stderr the string ` 'Trace :' ` , followed by the [ ` util.format() ` ] [ ]
185205formatted message and stack trace to the current position in the code.
186206
187- console.trace('Show me');
188- // Prints: (stack trace will vary based on where trace is called)
189- // Trace: Show me
190- // at repl:2:9
191- // at REPLServer.defaultEval (repl.js:248:27)
192- // at bound (domain.js:287:14)
193- // at REPLServer.runBound [as eval] (domain.js:300:12)
194- // at REPLServer.<anonymous> (repl.js:412:12)
195- // at emitOne (events.js:82:20)
196- // at REPLServer.emit (events.js:169:7)
197- // at REPLServer.Interface._onLine (readline.js:210:10)
198- // at REPLServer.Interface._line (readline.js:549:8)
199- // at REPLServer.Interface._ttyWrite (readline.js:826:14)
207+ ``` js
208+ console .trace (' Show me' );
209+ // Prints: (stack trace will vary based on where trace is called)
210+ // Trace: Show me
211+ // at repl:2:9
212+ // at REPLServer.defaultEval (repl.js:248:27)
213+ // at bound (domain.js:287:14)
214+ // at REPLServer.runBound [as eval] (domain.js:300:12)
215+ // at REPLServer.<anonymous> (repl.js:412:12)
216+ // at emitOne (events.js:82:20)
217+ // at REPLServer.emit (events.js:169:7)
218+ // at REPLServer.Interface._onLine (readline.js:210:10)
219+ // at REPLServer.Interface._line (readline.js:549:8)
220+ // at REPLServer.Interface._ttyWrite (readline.js:826:14)
221+ ```
200222
201223### console.warn([ data] [ , ... ] )
202224
0 commit comments