@alexbosworth/caporal
Version:
A full-featured framework for building command line applications (cli) with node.js
36 lines (29 loc) • 959 B
JavaScript
const winston = require('winston');
const util = require('util');
const prettyjson = require('@alexbosworth/prettyjson');
const CaporalTransport = function (options) {
options = options || {};
this.name = 'caporal';
this.level = options.level || 'info';
this._callback = options.callback || function(text, level){
//console.log("caporal-callback: %s: %s", level, text);
}
};
util.inherits(CaporalTransport, winston.Transport);
CaporalTransport.prototype.setCallback = function(callback) {
this._callback = callback;
};
CaporalTransport.prototype.log = function (level, msg, meta, callback) {
if (typeof meta === 'object' && Object.keys(meta).length) {
msg += "\n" + prettyjson.render(meta);
}
const levelInt = winston.levels[level];
this._callback(msg, levelInt <= 1 ? 'stderr' : 'stdout');
callback(null, true);
};
exports.logger = winston.createLogger({
transports: [
new (CaporalTransport)()
]
});
;