GeezerVison is showing github.com — console: colorize console error and warn · nodejs/node@db76c58
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: lib/internal/console/constructor.js +20-5Lines changed: 20 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | ArrayIsArray, | 9 | 9 | ArrayPrototypeForEach, | 10 | 10 | ArrayPrototypePush, | | 11 | + ArrayPrototypeSome, | 11 | 12 | ArrayPrototypeUnshift, | 12 | 13 | Boolean, | 13 | 14 | ErrorCaptureStackTrace, | @@ -67,6 +68,7 @@ const { | 67 | 68 | CHAR_LOWERCASE_N: kTraceInstant, | 68 | 69 | CHAR_UPPERCASE_C: kTraceCount, | 69 | 70 | } = require('internal/constants'); | | 71 | +const { styleText } = require('util'); | 70 | 72 | const kCounts = Symbol('counts'); | 71 | 73 |
| 72 | 74 | const kTraceConsoleCategory = 'node,node.console'; | @@ -273,7 +275,7 @@ ObjectDefineProperties(Console.prototype, { | 273 | 275 | [kWriteToConsole]: { | 274 | 276 | __proto__: null, | 275 | 277 | ...consolePropAttributes, | 276 | | - value: function(streamSymbol, string) { | | 278 | + value: function(streamSymbol, string, color = '') { | 277 | 279 | const ignoreErrors = this._ignoreErrors; | 278 | 280 | const groupIndent = this[kGroupIndent]; | 279 | 281 |
| @@ -288,6 +290,11 @@ ObjectDefineProperties(Console.prototype, { | 288 | 290 | } | 289 | 291 | string = groupIndent + string; | 290 | 292 | } | | 293 | + | | 294 | + if (color) { | | 295 | + string = styleText(color, string); | | 296 | + } | | 297 | + | 291 | 298 | string += '\n'; | 292 | 299 |
| 293 | 300 | if (ignoreErrors === false) return stream.write(string); | @@ -378,12 +385,15 @@ const consoleMethods = { | 378 | 385 | log(...args) { | 379 | 386 | this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args)); | 380 | 387 | }, | 381 | | - | 382 | | - | 383 | 388 | warn(...args) { | 384 | | - this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args)); | | 389 | + const color = (shouldColorize(args) && 'yellow') || ''; | | 390 | + this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color); | 385 | 391 | }, | 386 | 392 |
| | 393 | + error(...args) { | | 394 | + const color = (shouldColorize(args) && 'red') || ''; | | 395 | + this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color); | | 396 | + }, | 387 | 397 |
| 388 | 398 | dir(object, options) { | 389 | 399 | this[kWriteToConsole](kUseStdout, inspect(object, { | @@ -681,6 +691,12 @@ const iterKey = '(iteration index)'; | 681 | 691 |
| 682 | 692 | const isArray = (v) => ArrayIsArray(v) || isTypedArray(v) || isBuffer(v); | 683 | 693 |
| | 694 | +// TODO: remove string type check once the styleText supports objects | | 695 | +// Return true if all args are type string | | 696 | +const shouldColorize = (args) => { | | 697 | + return lazyUtilColors().hasColors && !ArrayPrototypeSome(args, (arg) => typeof arg !== 'string'); | | 698 | +}; | | 699 | + | 684 | 700 | function noop() {} | 685 | 701 |
| 686 | 702 | for (const method of ReflectOwnKeys(consoleMethods)) | @@ -689,7 +705,6 @@ for (const method of ReflectOwnKeys(consoleMethods)) | 689 | 705 | Console.prototype.debug = Console.prototype.log; | 690 | 706 | Console.prototype.info = Console.prototype.log; | 691 | 707 | Console.prototype.dirxml = Console.prototype.log; | 692 | | -Console.prototype.error = Console.prototype.warn; | 693 | 708 | Console.prototype.groupCollapsed = Console.prototype.group; | 694 | 709 |
| 695 | 710 | function initializeGlobalConsole(globalConsole) { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-repl.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -770,6 +770,7 @@ const errorTests = [ | 770 | 770 | 'Object [console] {', | 771 | 771 | ' log: [Function: log],', | 772 | 772 | ' warn: [Function: warn],', | | 773 | + ' error: [Function: error],', | 773 | 774 | ' dir: [Function: dir],', | 774 | 775 | ' time: [Function: time],', | 775 | 776 | ' timeEnd: [Function: timeEnd],', | @@ -785,7 +786,6 @@ const errorTests = [ | 785 | 786 | / {2}debug: \[Function: (debug|log)],/, | 786 | 787 | / {2}info: \[Function: (info|log)],/, | 787 | 788 | / {2}dirxml: \[Function: (dirxml|log)],/, | 788 | | - / {2}error: \[Function: (error|warn)],/, | 789 | 789 | / {2}groupCollapsed: \[Function: (groupCollapsed|group)],/, | 790 | 790 | / {2}Console: \[Function: Console],?/, | 791 | 791 | ...process.features.inspector ? [ | |
Collapse file Copy file name to clipboard +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 |
| 2 | | -(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set. | 3 | | -(Use `* --trace-warnings ...` to show where the warning was created) | | 2 | +*(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set. | | 3 | +(Use `* --trace-warnings ...` to show where the warning was created)* |
Collapse file Copy file name to clipboard +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 |
| 2 | | -(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. | 3 | | -(Use `* --trace-warnings ...` to show where the warning was created) | | 2 | +*(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. | | 3 | +(Use `* --trace-warnings ...` to show where the warning was created)* |
Collapse file Copy file name to clipboard +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. | 2 | | -(Use `* --trace-warnings ...` to show where the warning was created) | | 1 | +*(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. | | 2 | +(Use `* --trace-warnings ...` to show where the warning was created)* |
|