Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c9a211a

Browse filesBrowse files
Qardaduh95
authored andcommitted
diagnostics_channel: capture console messages
PR-URL: #56292 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent f600466 commit c9a211a
Copy full SHA for c9a211a

File tree

Expand file treeCollapse file tree

4 files changed

+149
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+149
-6
lines changed
Open diff view settings
Collapse file

‎doc/api/diagnostics_channel.md‎

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 numberDiff line numberDiff line change
@@ -1121,6 +1121,43 @@ While the diagnostics\_channel API is now considered stable, the built-in
11211121
channels currently available are not. Each channel must be declared stable
11221122
independently.
11231123

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+
11241161
#### HTTP
11251162

11261163
`http.client.request.created`
Collapse file

‎lib/internal/console/constructor.js‎

Copy file name to clipboardExpand all lines: lib/internal/console/constructor.js
+32-3Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ const {
6161
} = require('internal/constants');
6262
const kCounts = Symbol('counts');
6363
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');
6471

6572
const kTraceConsoleCategory = 'node,node.console';
6673

@@ -371,14 +378,39 @@ function timeLogImpl(consoleRef, label, formatted, args) {
371378

372379
const consoleMethods = {
373380
log(...args) {
381+
if (onLog.hasSubscribers) {
382+
onLog.publish(args);
383+
}
374384
this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args));
375385
},
376386

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+
},
377400

378401
warn(...args) {
402+
if (onWarn.hasSubscribers) {
403+
onWarn.publish(args);
404+
}
379405
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args));
380406
},
381407

408+
error(...args) {
409+
if (onError.hasSubscribers) {
410+
onError.publish(args);
411+
}
412+
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args));
413+
},
382414

383415
dir(object, options) {
384416
this[kWriteToConsole](kUseStdout, inspect(object, {
@@ -614,10 +646,7 @@ function noop() {}
614646
for (const method of ReflectOwnKeys(consoleMethods))
615647
Console.prototype[method] = consoleMethods[method];
616648

617-
Console.prototype.debug = Console.prototype.log;
618-
Console.prototype.info = Console.prototype.log;
619649
Console.prototype.dirxml = Console.prototype.log;
620-
Console.prototype.error = Console.prototype.warn;
621650
Console.prototype.groupCollapsed = Console.prototype.group;
622651

623652
function initializeGlobalConsole(globalConsole) {
Collapse file
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

‎test/parallel/test-repl.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-repl.js
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,10 @@ const errorTests = [
794794
expect: [
795795
'Object [console] {',
796796
' log: [Function: log],',
797+
' info: [Function: info],',
798+
' debug: [Function: debug],',
797799
' warn: [Function: warn],',
800+
' error: [Function: error],',
798801
' dir: [Function: dir],',
799802
' time: [Function: time],',
800803
' timeEnd: [Function: timeEnd],',
@@ -807,10 +810,7 @@ const errorTests = [
807810
' group: [Function: group],',
808811
' groupEnd: [Function: groupEnd],',
809812
' table: [Function: table],',
810-
/ {2}debug: \[Function: (debug|log)],/,
811-
/ {2}info: \[Function: (info|log)],/,
812813
/ {2}dirxml: \[Function: (dirxml|log)],/,
813-
/ {2}error: \[Function: (error|warn)],/,
814814
/ {2}groupCollapsed: \[Function: (groupCollapsed|group)],/,
815815
/ {2}Console: \[Function: Console],?/,
816816
...process.features.inspector ? [

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.