forked from mirrors/Scribe.js
Allow multiple colors options
This commit is contained in:
parent
dc80fc955a
commit
7f65af38a7
1 changed files with 48 additions and 28 deletions
|
@ -6,7 +6,7 @@
|
||||||
util = require('util'),
|
util = require('util'),
|
||||||
EventEmitter = require('events').EventEmitter,
|
EventEmitter = require('events').EventEmitter,
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
colors = require('colors/safe');
|
colorsjs = require('colors/safe');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* consoleOriginal
|
* consoleOriginal
|
||||||
|
@ -74,6 +74,34 @@
|
||||||
return '[' + infos.filename + ':' + infos.line + ']';
|
return '[' + infos.filename + ':' + infos.line + ']';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* applyColors
|
||||||
|
*
|
||||||
|
* Apply style with colors.js on the console output
|
||||||
|
* @param {String} msg The msg to stylize
|
||||||
|
* @param {Array|String} colors The colors
|
||||||
|
*
|
||||||
|
* @return {String}
|
||||||
|
*/
|
||||||
|
var applyColors = function (msg, colors) {
|
||||||
|
|
||||||
|
if (!colors) {
|
||||||
|
colors = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof colors === 'string') {
|
||||||
|
colors = [colors];
|
||||||
|
}
|
||||||
|
|
||||||
|
colors.forEach(function (color) {
|
||||||
|
if (typeof colorsjs[color] === 'function') {
|
||||||
|
msg = colorsjs[color](msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return msg;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Console2
|
* Console2
|
||||||
|
@ -87,7 +115,7 @@
|
||||||
* @param {int} opt.contextMediumSize Medium size of the context part of a log message.
|
* @param {int} opt.contextMediumSize Medium size of the context part of a log message.
|
||||||
* Used when calculating indent. Default to 45.
|
* Used when calculating indent. Default to 45.
|
||||||
* @param {int} opt.spaceSize Space between context part and log part. Default to 4.
|
* @param {int} opt.spaceSize Space between context part and log part. Default to 4.
|
||||||
* @param {String} opt.color Default color output for all loggers. Default cyan.
|
* @param {Array|String} opt.colors Default colors output for all loggers. Default ['cyan'].
|
||||||
*
|
*
|
||||||
* @param {Boolean} opt.alwaysTags Always print tags (even without tag() ). Default false.
|
* @param {Boolean} opt.alwaysTags Always print tags (even without tag() ). Default false.
|
||||||
* @param {Boolean} opt.alwaysLocation Always print location (even without file() ). Default false.
|
* @param {Boolean} opt.alwaysLocation Always print location (even without file() ). Default false.
|
||||||
|
@ -113,7 +141,7 @@
|
||||||
contextMediumSize : opt.contextMediumSize || 45,
|
contextMediumSize : opt.contextMediumSize || 45,
|
||||||
spaceSize : opt.spaceSize || 4,
|
spaceSize : opt.spaceSize || 4,
|
||||||
|
|
||||||
color : opt.color || "cyan",
|
colors : opt.color || "cyan",
|
||||||
|
|
||||||
alwaysTags : opt.alwaysTags || false,
|
alwaysTags : opt.alwaysTags || false,
|
||||||
alwaysLocation : opt.alwaysLocation || false,
|
alwaysLocation : opt.alwaysLocation || false,
|
||||||
|
@ -297,7 +325,7 @@
|
||||||
* You can then use it with console.myNewLogger
|
* You can then use it with console.myNewLogger
|
||||||
*
|
*
|
||||||
* @param {String} name The name of the logger.
|
* @param {String} name The name of the logger.
|
||||||
* @param {String} color Optional. Color of the console output. Default cyan.
|
* @param {String} colors Optional. Colors of the console output. Default cyan.
|
||||||
* See text colors from https://github.com/Marak/colors.js
|
* See text colors from https://github.com/Marak/colors.js
|
||||||
*
|
*
|
||||||
* @param {Object} opt Optional options object. @see Console2 opt for default values.
|
* @param {Object} opt Optional options object. @see Console2 opt for default values.
|
||||||
|
@ -308,7 +336,7 @@
|
||||||
* @param {Boolean} opt.alwaysTime Always print time (even without time() )
|
* @param {Boolean} opt.alwaysTime Always print time (even without time() )
|
||||||
* @param {Boolean} opt.alwaysDate Always print date (even without date() )
|
* @param {Boolean} opt.alwaysDate Always print date (even without date() )
|
||||||
*/
|
*/
|
||||||
Console2.prototype.addLogger = function (name, color, opt) {
|
Console2.prototype.addLogger = function (name, colors, opt) {
|
||||||
|
|
||||||
if (!opt) {
|
if (!opt) {
|
||||||
opt = {};
|
opt = {};
|
||||||
|
@ -319,7 +347,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
opt.name = name;
|
opt.name = name;
|
||||||
opt.color = color || this.opt.color;
|
opt.colors = colors || this.opt.colors;
|
||||||
opt.type = opt.type || opt.name;
|
opt.type = opt.type || opt.name;
|
||||||
opt.logInConsole = opt.logInConsole || this.opt.logInConsole;
|
opt.logInConsole = opt.logInConsole || this.opt.logInConsole;
|
||||||
opt.logInFile = opt.logInFile || this.opt.logInFile;
|
opt.logInFile = opt.logInFile || this.opt.logInFile;
|
||||||
|
@ -328,6 +356,7 @@
|
||||||
opt.alwaysTime = opt.alwaysTime || this.opt.alwaysTime;
|
opt.alwaysTime = opt.alwaysTime || this.opt.alwaysTime;
|
||||||
opt.alwaysDate = opt.alwaysDate || this.opt.alwaysDate;
|
opt.alwaysDate = opt.alwaysDate || this.opt.alwaysDate;
|
||||||
|
|
||||||
|
|
||||||
this[name] = function () {
|
this[name] = function () {
|
||||||
|
|
||||||
var location = getLocation();
|
var location = getLocation();
|
||||||
|
@ -357,22 +386,13 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
//Emit events
|
//Emit events
|
||||||
this.emit('new', log);
|
this.emit('new', log, log.type);
|
||||||
this.emit(opt.type || name, log);
|
this.emit(log.type, log);
|
||||||
|
|
||||||
//If the logger should print the message
|
//If the logger should print the message
|
||||||
//Print it
|
//Print it
|
||||||
if (opt.logInConsole) {
|
if (opt.logInConsole) {
|
||||||
|
global.console.log(applyColors(log.message, opt.colors));
|
||||||
var msg;
|
|
||||||
|
|
||||||
if (typeof colors[opt.color] === 'function') {
|
|
||||||
msg = colors[opt.color](log.message);
|
|
||||||
} else {
|
|
||||||
msg = log.message;
|
|
||||||
}
|
|
||||||
|
|
||||||
global.console.log(msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._reset();
|
this._reset();
|
||||||
|
|
Loading…
Add table
Reference in a new issue