From 46868fda352ee726c032a2eabe73ec0bf8e19ea2 Mon Sep 17 00:00:00 2001 From: artem-zakharchenko Date: Tue, 2 Apr 2019 11:53:11 +0200 Subject: [PATCH 1/2] refactor: upgrades "winston" package, adjusts loggers --- lib/logger.js | 18 +++++++------ lib/reporters/reporterOutputLogger.js | 34 +++++++++++++------------ package.json | 2 +- test/unit/reporters/CLIReporter-test.js | 10 -------- 4 files changed, 29 insertions(+), 35 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index 97e27005d..850c1e58e 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -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', + }, + }), }); diff --git a/lib/reporters/reporterOutputLogger.js b/lib/reporters/reporterOutputLogger.js index 93b8cf03c..6d7b37eb0 100644 --- a/lib/reporters/reporterOutputLogger.js +++ b/lib/reporters/reporterOutputLogger.js @@ -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, @@ -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', + }, + }), }); diff --git a/package.json b/package.json index 1193952fe..e3b642eb0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/unit/reporters/CLIReporter-test.js b/test/unit/reporters/CLIReporter-test.js index 764d31cec..5a1557076 100644 --- a/test/unit/reporters/CLIReporter-test.js +++ b/test/unit/reporters/CLIReporter-test.js @@ -14,16 +14,6 @@ const CLIReporter = proxyquire('../../../lib/reporters/CLIReporter', { 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')); From dc867457d8b198a6ff01760b98fc4fbb34845f7c Mon Sep 17 00:00:00 2001 From: artem-zakharchenko Date: Tue, 2 Apr 2019 16:19:57 +0200 Subject: [PATCH 2/2] refactor: wip --- lib/configuration.js | 97 +++++++++++++++++++------ test/integration/helpers.js | 45 ++++++++---- test/integration/proxy-test.js | 1 + test/unit/reporters/CLIReporter-test.js | 1 + 4 files changed, 109 insertions(+), 35 deletions(-) diff --git a/lib/configuration.js b/lib/configuration.js index 59638bce8..9ac2b57f2 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -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'); @@ -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'; + // } } diff --git a/test/integration/helpers.js b/test/integration/helpers.js index c9abe976b..2433dd11d 100644 --- a/test/integration/helpers.js +++ b/test/integration/helpers.js @@ -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'); @@ -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); }); } diff --git a/test/integration/proxy-test.js b/test/integration/proxy-test.js index a8010a24c..103312ea7 100644 --- a/test/integration/proxy-test.js +++ b/test/integration/proxy-test.js @@ -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); })); diff --git a/test/unit/reporters/CLIReporter-test.js b/test/unit/reporters/CLIReporter-test.js index 5a1557076..154e728fd 100644 --- a/test/unit/reporters/CLIReporter-test.js +++ b/test/unit/reporters/CLIReporter-test.js @@ -11,6 +11,7 @@ const CLIReporter = proxyquire('../../../lib/reporters/CLIReporter', { './reporterOutputLogger': reporterOutputLoggerStub, }); + describe('CLIReporter', () => { let test = {};