From 790b893d69a0618f94dd074d7fc690cbc39f0a6d Mon Sep 17 00:00:00 2001 From: Mohamed Akram Date: Mon, 10 Jun 2024 17:58:11 +0400 Subject: [PATCH] fix: logging to debug console --- docs/transports.md | 3 ++- lib/winston/transports/console.js | 18 ++++++++++++++++++ lib/winston/transports/index.d.ts | 2 ++ test/unit/winston/transports/console.test.js | 8 ++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/transports.md b/docs/transports.md index 2b2b8b1d3..6352f0dd3 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -92,7 +92,8 @@ The Console transport takes a few simple options: * __silent:__ Boolean flag indicating whether to suppress output (default false). * __eol:__ string indicating the end-of-line characters to use (default `os.EOL`) * __stderrLevels__ Array of strings containing the levels to log to stderr instead of stdout, for example `['error', 'debug', 'info']`. (default `[]`) -* __consoleWarnLevels__ Array of strings containing the levels to log using console.warn or to stderr (in Node.js) instead of stdout, for example `['warn', 'debug']`. (default `[]`) +* __consoleLevels__ Map of levels to their corresponding console level (default: see [`Console`](../lib/winston/transports/console.js)) +* __consoleWarnLevels__ **DEPRECATED** Array of strings containing the levels to log using console.warn or to stderr (in Node.js) instead of stdout, for example `['warn', 'debug']`. (default `[]`) ### File Transport ``` js diff --git a/lib/winston/transports/console.js b/lib/winston/transports/console.js index b54f3da9c..32c611155 100644 --- a/lib/winston/transports/console.js +++ b/lib/winston/transports/console.js @@ -9,6 +9,7 @@ 'use strict'; const os = require('os'); +const inspector = require('inspector'); const { LEVEL, MESSAGE } = require('triple-beam'); const TransportStream = require('winston-transport'); @@ -30,6 +31,20 @@ module.exports = class Console extends TransportStream { this.name = options.name || 'console'; this.stderrLevels = this._stringArrayToSet(options.stderrLevels); this.consoleWarnLevels = this._stringArrayToSet(options.consoleWarnLevels); + this.consoleLevels = options.consoleLevels || { + emerg: 'error', + alert: 'error', + crit: 'error', + error: 'error', + warn: 'warn', + warning: 'warn', + notice: 'info', + info: 'info', + http: 'debug', + verbose: 'debug', + debug: 'debug', + silly: 'debug' + }; this.eol = (typeof options.eol === 'string') ? options.eol : os.EOL; this.setMaxListeners(30); @@ -49,6 +64,7 @@ module.exports = class Console extends TransportStream { if (console._stderr) { // Node.js maps `process.stderr` to `console._stderr`. console._stderr.write(`${info[MESSAGE]}${this.eol}`); + inspector.console[this.consoleLevels[info[LEVEL]] || 'error'](info[MESSAGE]); } else { // console.error adds a newline console.error(info[MESSAGE]); @@ -63,6 +79,7 @@ module.exports = class Console extends TransportStream { // Node.js maps `process.stderr` to `console._stderr`. // in Node.js console.warn is an alias for console.error console._stderr.write(`${info[MESSAGE]}${this.eol}`); + inspector.console.warn(info[MESSAGE]); } else { // console.warn adds a newline console.warn(info[MESSAGE]); @@ -77,6 +94,7 @@ module.exports = class Console extends TransportStream { if (console._stdout) { // Node.js maps `process.stdout` to `console._stdout`. console._stdout.write(`${info[MESSAGE]}${this.eol}`); + inspector.console[this.consoleLevels[info[LEVEL]] || 'log'](info[MESSAGE]); } else { // console.log adds a newline. console.log(info[MESSAGE]); diff --git a/lib/winston/transports/index.d.ts b/lib/winston/transports/index.d.ts index 2eb7f1122..38ea1955c 100644 --- a/lib/winston/transports/index.d.ts +++ b/lib/winston/transports/index.d.ts @@ -9,6 +9,8 @@ import * as Transport from 'winston-transport'; declare namespace winston { interface ConsoleTransportOptions extends Transport.TransportStreamOptions { + consoleLevels?: Record, + /** @deprecated Use `consoleLevels` instead */ consoleWarnLevels?: string[], stderrLevels?: string[]; debugStdout?: boolean; diff --git a/test/unit/winston/transports/console.test.js b/test/unit/winston/transports/console.test.js index b6f4aeed5..4ef81a4d3 100644 --- a/test/unit/winston/transports/console.test.js +++ b/test/unit/winston/transports/console.test.js @@ -25,6 +25,9 @@ const transports = { consoleWarnLevels: new winston.transports.Console({ consoleWarnLevels: ['warn', 'debug'] }), + consoleLevels: new winston.transports.Console({ + consoleLevels: {'warning': 'warn'} + }), eol: new winston.transports.Console({ eol: 'X' }), syslog: new winston.transports.Console({ levels: winston.config.syslog.levels @@ -119,6 +122,11 @@ describe('Console transport', function () { ['warn', 'debug'], 'consoleWarnLevels' )); + it("{ consoleLevels: {'warning': 'warn'} } logs to them appropriately", assertLogLevelsValues( + transports.consoleLevels, + ['warning'], + 'consoleLevels' + )); it('{ eol } adds a custom EOL delimiter', function (done) { stdMocks.use();