diff --git a/packages/logger/lib/index.js b/packages/logger/lib/index.js index cff7e1d..a78e5b5 100644 --- a/packages/logger/lib/index.js +++ b/packages/logger/lib/index.js @@ -15,12 +15,34 @@ 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(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}` 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" } }