Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

refactor: upgrades "winston" package, adjusts loggers #1303

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
97 changes: 76 additions & 21 deletions lib/configuration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const clone = require('clone');
const { EventEmitter } = require('events');
const { format } = require('winston')

const logger = require('./logger');
const reporterOutputLogger = require('./reporters/reporterOutputLogger');
Expand All @@ -20,33 +21,87 @@ function coerceToBoolean(value) {
return false;
}

function validateLogLevel(level, levels) {
if (!levels.includes(level)) {
throw new Error(`The logging level '${level}' is unsupported, `
+ 'supported are: silent, error, warning, debug');
}

return true;
}

/**
* @returns {string}
*/
function getLogLevel(options) {
const supportedLogLevels = [
'silent',
'warning',
'warn',
'debug',
'error',
];
const { loglevel } = options;
const defaultLogLevel = 'warn';

return loglevel && validateLogLevel(loglevel, supportedLogLevels)
? loglevel
: defaultLogLevel;
}

function applyLoggingOptions(options) {
if (options.color === false) {
logger.transports.console.colorize = false;
reporterOutputLogger.transports.console.colorize = false;
[logger, reporterOutputLogger].forEach((loggerInstance) => {
loggerInstance.configure({
format: format.colorize(false),
});
});
// logger.transports.console.colorize = false;
// reporterOutputLogger.transports.console.colorize = false;
}

// Handling the 'loglevel' value
if (options.loglevel) {
const loglevel = options.loglevel.toLowerCase();
if (loglevel === 'silent') {
logger.transports.console.silent = true;
} else if (loglevel === 'warning') {
logger.transports.console.level = 'warn';
} else if (loglevel === 'debug') {
logger.transports.console.level = 'debug';
logger.transports.console.timestamp = true;
} else if (['warn', 'error'].includes(loglevel)) {
logger.transports.console.level = loglevel;
} else {
logger.transports.console.level = 'warn';
throw new Error(`The logging level '${loglevel}' is unsupported, `
+ 'supported are: silent, error, warning, debug');
}
} else {
logger.transports.console.level = 'warn';
const loglevel = getLogLevel(options);

switch (loglevel) {
case 'silent':
logger.configure({ silent: true });
break;

case 'warning':
logger.configure({ level: 'warn' });
break;

case 'debug':
logger.configure({
level: loglevel,
format: format.timestamp(),
});
break;

default:
logger.configure({ level: loglevel });
}

// Handling the 'loglevel' value
// if (logLevel) {
// const loglevel = options.loglevel.toLowerCase();
// if (loglevel === 'silent') {
// logger.transports.console.silent = true;
// } else if (loglevel === 'warning') {
// logger.transports.console.level = 'warn';
// } else if (loglevel === 'debug') {
// logger.transports.console.level = 'debug';
// logger.transports.console.timestamp = true;
// } else if (['warn', 'error'].includes(loglevel)) {
// logger.transports.console.level = loglevel;
// } else {
// logger.transports.console.level = 'warn';
// throw new Error(`The logging level '${loglevel}' is unsupported, `
// + 'supported are: silent, error, warning, debug');
// }
// } else {
// logger.transports.console.level = 'warn';
// }
}


Expand Down
18 changes: 10 additions & 8 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const winston = require('winston');
const { createLogger, transports, format } = require('winston');

module.exports = new (winston.Logger)({
module.exports = createLogger({
transports: [
new (winston.transports.Console)({ colorize: true }),
new transports.Console({ colorize: true }),
],
levels: {
debug: 2,
warn: 1,
error: 0,
},
colors: {
debug: 'cyan',
warn: 'yellow',
error: 'red',
},
format: format.colorize({
colors: {
debug: 'cyan',
warn: 'yellow',
error: 'red',
},
}),
});
34 changes: 18 additions & 16 deletions lib/reporters/reporterOutputLogger.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const winston = require('winston');
const { createLogger, transports, format } = require('winston');

module.exports = new (winston.Logger)({
module.exports = createLogger({
transports: [
new (winston.transports.Console)({ colorize: true, level: 'info' }),
new transports.Console({ colorize: true, level: 'info' }),
],
levels: {
info: 10,
Expand All @@ -17,17 +17,19 @@ module.exports = new (winston.Logger)({
skip: 1,
error: 0,
},
colors: {
info: 'blue',
test: 'yellow',
pass: 'green',
fail: 'red',
complete: 'green',
actual: 'red',
expected: 'red',
hook: 'green',
request: 'green',
skip: 'yellow',
error: 'red',
},
format: format.colorize({
colors: {
info: 'blue',
test: 'yellow',
pass: 'green',
fail: 'red',
complete: 'green',
actual: 'red',
expected: 'red',
hook: 'green',
request: 'green',
skip: 'yellow',
error: 'red',
},
}),
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"untildify": "3.0.3",
"uuid": "3.3.2",
"which": "1.3.1",
"winston": "2.4.0"
"winston": "3.2.1"
},
"devDependencies": {
"@commitlint/cli": "7.5.2",
Expand Down
45 changes: 31 additions & 14 deletions test/integration/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const async = require('async');
const sinon = require('sinon');
const bodyParser = require('body-parser');
const clone = require('clone');
const express = require('express');
Expand All @@ -23,28 +24,44 @@ const DREDD_BIN = require.resolve('../../bin/dredd');
// from the 'fn' function
// - logging (string) - the recorded logging output
function recordLogging(fn, callback) {
const loggerSilent = !!logger.transports.console.silent;
const reporterOutputLoggerSilent = !!reporterOutputLogger.transports.console.silent;
let logging = '';
const loggerSilent = !!logger.silent;
const reporterOutputLoggerSilent = !!reporterOutputLogger.silent;

// Supress Dredd's console output (remove if debugging)
logger.transports.console.silent = true;
reporterOutputLogger.transports.console.silent = true;
[logger, reporterOutputLogger].forEach((loggerInstance) => {
loggerInstance.configure({ silent: true });

if (!loggerInstance.log.wrappedMethod) {
sinon.stub(loggerInstance, 'log').callsFake((level, message) => {
const nextLogging = `${level}: ${message}\n`;
console.log({nextLogging})
logging += nextLogging
});
}
});

let logging = '';
const record = (transport, level, message) => {
logging += `${level}: ${message}\n`;
};
// logger.transports.console.silent = true;
// reporterOutputLogger.transports.console.silent = true;

// const record = (transport, level, message) => {
// logging += `${level}: ${message}\n`;
// };

logger.on('logging', record);
reporterOutputLogger.on('logging', record);
/**
* @todo "logging" is deprecated, replace.
*/
// logger.on('logging', record);
// reporterOutputLogger.on('logging', record);

fn((...args) => {
logger.removeListener('logging', record);
logger.transports.console.silent = loggerSilent;
// logger.removeListener('logging', record);
logger.configure({ silent: loggerSilent });

reporterOutputLogger.removeListener('logging', record);
reporterOutputLogger.transports.console.silent = reporterOutputLoggerSilent;
// reporterOutputLogger.removeListener('logging', record);
reporterOutputLogger.configure({ silent: reporterOutputLoggerSilent });

console.log('callbacking', logging)
callback(null, args, logging);
});
}
Expand Down
1 change: 1 addition & 0 deletions test/integration/proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function createAndRunDredd(configuration, done) {
dredd.configuration.http.strictSSL = false;
next();
}, (err, args, dreddInitLogging) => runDredd(dredd, (error, info) => {
console.log({dreddInitLogging, info})
info.logging = `${dreddInitLogging}\n${info.logging}`;
done(error, info);
}));
Expand Down
11 changes: 1 addition & 10 deletions test/unit/reporters/CLIReporter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,10 @@ const CLIReporter = proxyquire('../../../lib/reporters/CLIReporter', {
'./reporterOutputLogger': reporterOutputLoggerStub,
});


describe('CLIReporter', () => {
let test = {};

before(() => {
loggerStub.transports.console.silent = true;
reporterOutputLoggerStub.transports.console.silent = true;
});

after(() => {
loggerStub.transports.console.silent = false;
reporterOutputLoggerStub.transports.console.silent = false;
});

describe('when starting', () => {
beforeEach(() => sinon.spy(loggerStub, 'debug'));

Expand Down