GeezerVison is showing github.com — diagnostics_channel: capture console messages · nodejs/node@c9a211a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/diagnostics_channel.md +37Lines changed: 37 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1121,6 +1121,43 @@ While the diagnostics\_channel API is now considered stable, the built-in | 1121 | 1121 | channels currently available are not. Each channel must be declared stable | 1122 | 1122 | independently. | 1123 | 1123 |
| | 1124 | +#### Console | | 1125 | + | | 1126 | +`console.log` | | 1127 | + | | 1128 | +* `args` {any\[]} | | 1129 | + | | 1130 | +Emitted when `console.log()` is called. Receives and array of the arguments | | 1131 | +passed to `console.log()`. | | 1132 | + | | 1133 | +`console.info` | | 1134 | + | | 1135 | +* `args` {any\[]} | | 1136 | + | | 1137 | +Emitted when `console.info()` is called. Receives and array of the arguments | | 1138 | +passed to `console.info()`. | | 1139 | + | | 1140 | +`console.debug` | | 1141 | + | | 1142 | +* `args` {any\[]} | | 1143 | + | | 1144 | +Emitted when `console.debug()` is called. Receives and array of the arguments | | 1145 | +passed to `console.debug()`. | | 1146 | + | | 1147 | +`console.warn` | | 1148 | + | | 1149 | +* `args` {any\[]} | | 1150 | + | | 1151 | +Emitted when `console.warn()` is called. Receives and array of the arguments | | 1152 | +passed to `console.warn()`. | | 1153 | + | | 1154 | +`console.error` | | 1155 | + | | 1156 | +* `args` {any\[]} | | 1157 | + | | 1158 | +Emitted when `console.error()` is called. Receives and array of the arguments | | 1159 | +passed to `console.error()`. | | 1160 | + | 1124 | 1161 | #### HTTP | 1125 | 1162 |
| 1126 | 1163 | `http.client.request.created` | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/console/constructor.js +32-3Lines changed: 32 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,6 +61,13 @@ const { | 61 | 61 | } = require('internal/constants'); | 62 | 62 | const kCounts = Symbol('counts'); | 63 | 63 | const { time, timeLog, timeEnd, kNone } = require('internal/util/debuglog'); | | 64 | +const { channel } = require('diagnostics_channel'); | | 65 | + | | 66 | +const onLog = channel('console.log'); | | 67 | +const onWarn = channel('console.warn'); | | 68 | +const onError = channel('console.error'); | | 69 | +const onInfo = channel('console.info'); | | 70 | +const onDebug = channel('console.debug'); | 64 | 71 |
| 65 | 72 | const kTraceConsoleCategory = 'node,node.console'; | 66 | 73 |
| @@ -371,14 +378,39 @@ function timeLogImpl(consoleRef, label, formatted, args) { | 371 | 378 |
| 372 | 379 | const consoleMethods = { | 373 | 380 | log(...args) { | | 381 | + if (onLog.hasSubscribers) { | | 382 | + onLog.publish(args); | | 383 | + } | 374 | 384 | this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args)); | 375 | 385 | }, | 376 | 386 |
| | 387 | + info(...args) { | | 388 | + if (onInfo.hasSubscribers) { | | 389 | + onInfo.publish(args); | | 390 | + } | | 391 | + this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args)); | | 392 | + }, | | 393 | + | | 394 | + debug(...args) { | | 395 | + if (onDebug.hasSubscribers) { | | 396 | + onDebug.publish(args); | | 397 | + } | | 398 | + this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args)); | | 399 | + }, | 377 | 400 |
| 378 | 401 | warn(...args) { | | 402 | + if (onWarn.hasSubscribers) { | | 403 | + onWarn.publish(args); | | 404 | + } | 379 | 405 | this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args)); | 380 | 406 | }, | 381 | 407 |
| | 408 | + error(...args) { | | 409 | + if (onError.hasSubscribers) { | | 410 | + onError.publish(args); | | 411 | + } | | 412 | + this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args)); | | 413 | + }, | 382 | 414 |
| 383 | 415 | dir(object, options) { | 384 | 416 | this[kWriteToConsole](kUseStdout, inspect(object, { | @@ -614,10 +646,7 @@ function noop() {} | 614 | 646 | for (const method of ReflectOwnKeys(consoleMethods)) | 615 | 647 | Console.prototype[method] = consoleMethods[method]; | 616 | 648 |
| 617 | | -Console.prototype.debug = Console.prototype.log; | 618 | | -Console.prototype.info = Console.prototype.log; | 619 | 649 | Console.prototype.dirxml = Console.prototype.log; | 620 | | -Console.prototype.error = Console.prototype.warn; | 621 | 650 | Console.prototype.groupCollapsed = Console.prototype.group; | 622 | 651 |
| 623 | 652 | function initializeGlobalConsole(globalConsole) { | |
Collapse file Copy file name to clipboard +77Lines changed: 77 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const { mustCall } = require('../common'); | | 4 | +const { deepStrictEqual, ok, strictEqual } = require('assert'); | | 5 | + | | 6 | +const { channel } = require('diagnostics_channel'); | | 7 | + | | 8 | +const { | | 9 | + hijackStdout, | | 10 | + hijackStderr, | | 11 | + restoreStdout, | | 12 | + restoreStderr | | 13 | +} = require('../common/hijackstdio'); | | 14 | + | | 15 | +const stdoutMethods = [ | | 16 | + 'log', | | 17 | + 'info', | | 18 | + 'debug', | | 19 | +]; | | 20 | + | | 21 | +const stderrMethods = [ | | 22 | + 'warn', | | 23 | + 'error', | | 24 | +]; | | 25 | + | | 26 | +const methods = [ | | 27 | + ...stdoutMethods, | | 28 | + ...stderrMethods, | | 29 | +]; | | 30 | + | | 31 | +const channels = { | | 32 | + log: channel('console.log'), | | 33 | + info: channel('console.info'), | | 34 | + debug: channel('console.debug'), | | 35 | + warn: channel('console.warn'), | | 36 | + error: channel('console.error') | | 37 | +}; | | 38 | + | | 39 | +process.stdout.isTTY = false; | | 40 | +process.stderr.isTTY = false; | | 41 | + | | 42 | +for (const method of methods) { | | 43 | + let intercepted = false; | | 44 | + let formatted = false; | | 45 | + | | 46 | + const isStdout = stdoutMethods.includes(method); | | 47 | + const hijack = isStdout ? hijackStdout : hijackStderr; | | 48 | + const restore = isStdout ? restoreStdout : restoreStderr; | | 49 | + | | 50 | + const foo = 'string'; | | 51 | + const bar = { key: /value/ }; | | 52 | + const baz = [ 1, 2, 3 ]; | | 53 | + | | 54 | + channels[method].subscribe(mustCall((args) => { | | 55 | + // Should not have been formatted yet. | | 56 | + intercepted = true; | | 57 | + ok(!formatted); | | 58 | + | | 59 | + // Should receive expected log message args. | | 60 | + deepStrictEqual(args, [foo, bar, baz]); | | 61 | + | | 62 | + // Should be able to mutate message args and have it reflected in output. | | 63 | + bar.added = true; | | 64 | + })); | | 65 | + | | 66 | + hijack(mustCall((output) => { | | 67 | + // Should have already been intercepted. | | 68 | + formatted = true; | | 69 | + ok(intercepted); | | 70 | + | | 71 | + // Should produce expected formatted output with mutated message args. | | 72 | + strictEqual(output, 'string { key: /value/, added: true } [ 1, 2, 3 ]\n'); | | 73 | + })); | | 74 | + | | 75 | + console[method](foo, bar, baz); | | 76 | + restore(); | | 77 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-repl.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -794,7 +794,10 @@ const errorTests = [ | 794 | 794 | expect: [ | 795 | 795 | 'Object [console] {', | 796 | 796 | ' log: [Function: log],', | | 797 | + ' info: [Function: info],', | | 798 | + ' debug: [Function: debug],', | 797 | 799 | ' warn: [Function: warn],', | | 800 | + ' error: [Function: error],', | 798 | 801 | ' dir: [Function: dir],', | 799 | 802 | ' time: [Function: time],', | 800 | 803 | ' timeEnd: [Function: timeEnd],', | @@ -807,10 +810,7 @@ const errorTests = [ | 807 | 810 | ' group: [Function: group],', | 808 | 811 | ' groupEnd: [Function: groupEnd],', | 809 | 812 | ' table: [Function: table],', | 810 | | - / {2}debug: \[Function: (debug|log)],/, | 811 | | - / {2}info: \[Function: (info|log)],/, | 812 | 813 | / {2}dirxml: \[Function: (dirxml|log)],/, | 813 | | - / {2}error: \[Function: (error|warn)],/, | 814 | 814 | / {2}groupCollapsed: \[Function: (groupCollapsed|group)],/, | 815 | 815 | / {2}Console: \[Function: Console],?/, | 816 | 816 | ...process.features.inspector ? [ | |
|