Skip to content

Commit

Permalink
fix: logging to debug console
Browse files Browse the repository at this point in the history
  • Loading branch information
mohd-akram committed Jun 10, 2024
1 parent 7f5f014 commit 790b893
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/winston/transports/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand Down
2 changes: 2 additions & 0 deletions lib/winston/transports/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import * as Transport from 'winston-transport';

declare namespace winston {
interface ConsoleTransportOptions extends Transport.TransportStreamOptions {
consoleLevels?: Record<string, string>,
/** @deprecated Use `consoleLevels` instead */
consoleWarnLevels?: string[],
stderrLevels?: string[];
debugStdout?: boolean;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/winston/transports/console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 790b893

Please sign in to comment.