Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: logging to debug console #2474

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 || {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be added to index.d.ts as a property on ConsoleTransportOptions.

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