From 5450469f3a116f1430cd7cdaa53d67e97d2781bd Mon Sep 17 00:00:00 2001 From: Navy Date: Tue, 14 Apr 2020 00:28:22 +0300 Subject: [PATCH] console transport extension --- .../logger/transports/ExtendedConsole.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 middleware/logger/transports/ExtendedConsole.js diff --git a/middleware/logger/transports/ExtendedConsole.js b/middleware/logger/transports/ExtendedConsole.js new file mode 100644 index 0000000..af5a2e5 --- /dev/null +++ b/middleware/logger/transports/ExtendedConsole.js @@ -0,0 +1,56 @@ +const Transport = require('winston-transport'); +const moment = require('moment'); +const chalk = require('chalk'); + +const Constants = { + Colors: { + error: 'red', + warn: 'yellow', + info: 'blue', + verbose: 'cyan', + debug: 'magenta', + silly: 'magentaBright' + } +}; + +class ExtendedConsole extends Transport { + + constructor(opts) { + super(opts); + // + // Consume any custom options here. e.g.: + // - Connection information for databases + // - Authentication information for APIs (e.g. loggly, papertrail, + // logentries, etc.). + // + } + + log(info, callback) { + setImmediate(() => { + this.emit('logged', info); + }); + + //console.log('Extended console:'); + //console.log(info); + + const hdr = info.message.match(/^(\[[0-9\-: ]{19}\](\[shard-[0-9]{2}\])?)/); + if(hdr) info.message = info.message.replace(hdr[1], '').trim(); + + const color = Constants.Colors[info.level]; + const header = `${chalk[color](hdr ? hdr[1] : `[${this.date}]`)}`; + + console.log(`${header} ${info.message}`); + + if (callback) { + callback(); // eslint-disable-line callback-return + } + + } + + get date() { + return moment().format("MM-DD-YYYY hh:mm:ss"); + } + +} + +module.exports = ExtendedConsole; \ No newline at end of file