From 9264345ae1e5137716939b79206fe35f36a6f423 Mon Sep 17 00:00:00 2001 From: barslev Date: Thu, 19 Mar 2020 15:10:52 +0100 Subject: [PATCH 1/2] Updates winston to version 3. --- package.json | 2 +- src/utils/logger.js | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a54e944..76f2bb2 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "has": "^1.0.3", "lru-cache": "^4.1.5", "object.assign": "^4.1.0", - "winston": "^2.4.4" + "winston": "^3.0.0" }, "engines": { "node": ">= 0.10" diff --git a/src/utils/logger.js b/src/utils/logger.js index f6ea0ac..c3992b4 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -16,9 +16,29 @@ const loggerInterface = { } else { const options = { ...OPTIONS, ...config }; - logger = new winston.Logger({ + logger = winston.createLogger({ transports: [ - new winston.transports.Console(options), + new winston.transports.Console((function(opts) { + const newOpts = {}; + const formatArray = []; + const formatOptions = { + stringify: () => winston.format((info) => { info.message = JSON.stringify(info.message); })(), + formatter: () => winston.format((info) => { info.message = opts.formatter(Object.assign(info, opts)); })(), + json: () => winston.format.json(), + raw: () => winston.format.json(), + label: () => winston.format.label(opts.label), + logstash: () => winston.format.logstash(), + prettyPrint: () => winston.format.prettyPrint({depth: opts.depth || 2}), + colorize: () => winston.format.colorize({level: opts.colorize === true || opts.colorize === 'level', all: opts.colorize === 'all', message: opts.colorize === 'message'}), + timestamp: () => winston.format.timestamp(), + align: () => winston.format.align(), + showLevel: () => winston.format((info) => { info.message = info.level + ': ' + info.message; })() + } + Object.keys(opts).filter(k => !formatOptions.hasOwnProperty(k)).forEach((k) => { newOpts[k] = opts[k]; }); + Object.keys(opts).filter(k => formatOptions.hasOwnProperty(k) && formatOptions[k]).forEach(k => formatArray.push(formatOptions[k]())); + newOpts.format = winston.format.combine(...formatArray); + return newOpts; + })(options)), ], }); } From 65425ddcb252f5c5dcbe3d9ad6cee9c77d744617 Mon Sep 17 00:00:00 2001 From: barslev Date: Thu, 19 Mar 2020 15:27:59 +0100 Subject: [PATCH 2/2] Fixed linting --- src/utils/logger.js | 53 +++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/utils/logger.js b/src/utils/logger.js index c3992b4..15c02af 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -9,6 +9,37 @@ const OPTIONS = { prettyPrint: process.env.NODE_ENV !== 'production', }; +function convertOptionsToWinstonV3(opts) { + const newOpts = {}; + const formatArray = []; + const formatOptions = { + stringify: () => winston.format((info) => { info.message = JSON.stringify(info.message); })(), // eslint-disable-line + formatter: () => winston.format( + (info) => { info.message = opts.formatter(Object.assign(info, opts)); })(), // eslint-disable-line + json: () => winston.format.json(), + raw: () => winston.format.json(), + label: () => winston.format.label(opts.label), + logstash: () => winston.format.logstash(), + prettyPrint: () => winston.format.prettyPrint({ depth: opts.depth || 2 }), + colorize: () => winston.format.colorize({ + level: opts.colorize === true || opts.colorize === 'level', + all: opts.colorize === 'all', + message: opts.colorize === 'message', + }), + timestamp: () => winston.format.timestamp(), + align: () => winston.format.align(), + showLevel: () => winston.format((info) => { info.message = `${info.level}: ${info.message}`; })(), // eslint-disable-line + }; + Object.keys(opts) + .filter(k => !Object.keys(formatOptions).includes(k)) + .forEach((k) => { newOpts[k] = opts[k]; }); + Object.keys(opts) + .filter(k => Object.keys(formatOptions).includes(k) && formatOptions[k]) + .forEach(k => formatArray.push(formatOptions[k]())); + newOpts.format = winston.format.combine(...formatArray); + return newOpts; +} + const loggerInterface = { init(config, loggerInstance) { if (loggerInstance) { @@ -18,27 +49,7 @@ const loggerInterface = { logger = winston.createLogger({ transports: [ - new winston.transports.Console((function(opts) { - const newOpts = {}; - const formatArray = []; - const formatOptions = { - stringify: () => winston.format((info) => { info.message = JSON.stringify(info.message); })(), - formatter: () => winston.format((info) => { info.message = opts.formatter(Object.assign(info, opts)); })(), - json: () => winston.format.json(), - raw: () => winston.format.json(), - label: () => winston.format.label(opts.label), - logstash: () => winston.format.logstash(), - prettyPrint: () => winston.format.prettyPrint({depth: opts.depth || 2}), - colorize: () => winston.format.colorize({level: opts.colorize === true || opts.colorize === 'level', all: opts.colorize === 'all', message: opts.colorize === 'message'}), - timestamp: () => winston.format.timestamp(), - align: () => winston.format.align(), - showLevel: () => winston.format((info) => { info.message = info.level + ': ' + info.message; })() - } - Object.keys(opts).filter(k => !formatOptions.hasOwnProperty(k)).forEach((k) => { newOpts[k] = opts[k]; }); - Object.keys(opts).filter(k => formatOptions.hasOwnProperty(k) && formatOptions[k]).forEach(k => formatArray.push(formatOptions[k]())); - newOpts.format = winston.format.combine(...formatArray); - return newOpts; - })(options)), + new winston.transports.Console(convertOptionsToWinstonV3(options)), ], }); }