From 5069010cfe310e21f95998a3efd957a8f9651da5 Mon Sep 17 00:00:00 2001 From: barslev Date: Wed, 11 Mar 2020 13:17:56 +0100 Subject: [PATCH 1/2] Updated winston to version 3 --- packages/logger/lib/index.js | 24 ++++++++++++++++++++++-- packages/logger/package.json | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/logger/lib/index.js b/packages/logger/lib/index.js index cff7e1d..baf82b1 100644 --- a/packages/logger/lib/index.js +++ b/packages/logger/lib/index.js @@ -15,9 +15,29 @@ winston.addColors({ error: 'red', warn: 'yellow' }) -const logger = new winston.Logger({ +const 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)) ] }) diff --git a/packages/logger/package.json b/packages/logger/package.json index ba14233..ed51347 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -19,6 +19,6 @@ "url": "https://github.com/staeco/modules/issues" }, "dependencies": { - "winston": "2" + "winston": "^3.0.0" } } From 9709c79c42b27c2c58020767f5de7df1d9225a6b Mon Sep 17 00:00:00 2001 From: barslev Date: Sun, 15 Mar 2020 21:17:32 +0100 Subject: [PATCH 2/2] Code cleanup --- packages/logger/lib/index.js | 44 +++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/logger/lib/index.js b/packages/logger/lib/index.js index baf82b1..a78e5b5 100644 --- a/packages/logger/lib/index.js +++ b/packages/logger/lib/index.js @@ -17,30 +17,32 @@ winston.addColors({ }) const 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)) ] }) +function convertOptionsToWinstonV3(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; +} + const format = (o) => { if (o && o.sql) { return `${o}\nSQL:\n${o.sql}`