| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0fa4d90 commit 66c7454
36 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,33 +15,37 @@ The module exports two specific components: | |||
| 15 | 15 | ||
| 16 | 16 | Example using the global `console`: | |
| 17 | 17 | ||
| 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 | + ``` | ||
| 28 | 30 | ||
| 29 | 31 | Example using the `Console` class: | |
| 30 | 32 | ||
| 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); | ||
| 34 | 37 | ||
| 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 | ||
| 41 | 44 | ||
| 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 | + ``` | ||
| 45 | 49 | ||
| 46 | 50 | While the API for the `Console` class is designed fundamentally around the | |
| 47 | 51 | Web 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). | |||
| 55 | 59 | ||
| 56 | 60 | In the following example, stdout is non-blocking while stderr is blocking: | |
| 57 | 61 | ||
| 58 | - $ node script.js 2> error.log | tee info.log | ||
| 62 | + ``` | ||
| 63 | + $ node script.js 2> error.log | tee info.log | ||
| 64 | + ``` | ||
| 59 | 65 | ||
| 60 | 66 | Typically, the distinction between blocking/non-blocking is not important | |
| 61 | 67 | unless 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 | |||
| 69 | 75 | output streams and can be accessed using either `require('console').Console` | |
| 70 | 76 | or `console.Console`: | |
| 71 | 77 | ||
| 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 | + ``` | ||
| 74 | 82 | ||
| 75 | 83 | ### new Console(stdout[, stderr]) | |
| 76 | 84 | ||
@@ -79,30 +87,36 @@ Creates a new `Console` by passing one or two writable stream instances. | |||
| 79 | 87 | is used for warning or error output. If `stderr` isn't passed, the warning | |
| 80 | 88 | and error output will be sent to the `stdout`. | |
| 81 | 89 | ||
| 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 | + ``` | ||
| 90 | 100 | ||
| 91 | 101 | The global `console` is a special `Console` whose output is sent to | |
| 92 | 102 | `process.stdout` and `process.stderr`. It is equivalent to calling: | |
| 93 | 103 | ||
| 94 | - new Console(process.stdout, process.stderr); | ||
| 104 | + ```js | ||
| 105 | + new Console(process.stdout, process.stderr); | ||
| 106 | + ``` | ||
| 95 | 107 | ||
| 96 | 108 | ### console.assert(value[, message][, ...]) | |
| 97 | 109 | ||
| 98 | 110 | A simple assertion test that verifies whether `value` is truthy. If it is not, | |
| 99 | 111 | an `AssertionError` is throw. If provided, the error `message` is formatted | |
| 100 | 112 | using [`util.format()`][] and used as the error message. | |
| 101 | 113 | ||
| 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 | + ``` | ||
| 106 | 120 | ||
| 107 | 121 | ### console.dir(obj[, options]) | |
| 108 | 122 | ||
@@ -129,11 +143,13 @@ used as the primary message and all additional used as substitution | |||
| 129 | 143 | values similar to `printf()` (the arguments are all passed to | |
| 130 | 144 | [`util.format()`][]). | |
| 131 | 145 | ||
| 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 | + ``` | ||
| 137 | 153 | ||
| 138 | 154 | If formatting elements (e.g. `%d`) are not found in the first string then | |
| 139 | 155 | [`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 | |||
| 150 | 166 | values similar to `printf()` (the arguments are all passed to | |
| 151 | 167 | [`util.format()`][]). | |
| 152 | 168 | ||
| 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 | + ``` | ||
| 158 | 176 | ||
| 159 | 177 | If formatting elements (e.g. `%d`) are not found in the first string then | |
| 160 | 178 | [`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. | |||
| 172 | 190 | Stops a timer that was previously started by calling [`console.time()`][] and | |
| 173 | 191 | prints the result to stdout: | |
| 174 | 192 | ||
| 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 | + ``` | ||
| 181 | 201 | ||
| 182 | 202 | ### console.trace(message[, ...]) | |
| 183 | 203 | ||
| 184 | 204 | Prints to stderr the string `'Trace :'`, followed by the [`util.format()`][] | |
| 185 | 205 | formatted message and stack trace to the current position in the code. | |
| 186 | 206 | ||
| 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 | + ``` | ||
| 200 | 222 | ||
| 201 | 223 | ### console.warn([data][, ...]) | |
| 202 | 224 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments