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'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
path = require('path'),
|
||||
colors = require('colors/safe');
|
||||
colorsjs = require('colors/safe');
|
||||
|
||||
/**
|
||||
* consoleOriginal
|
||||
|
@ -74,25 +74,53 @@
|
|||
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
|
||||
*
|
||||
* @constructor
|
||||
*
|
||||
* @param {Object} opt Optional default options for all loggers.
|
||||
* @param {Boolean} opt.logInConsole Should all loggers print to console by default ? Default true.
|
||||
* @param {Boolean} opt.logInFile Should all loggers saver log in file by default ? Default true.
|
||||
* @param {Object} opt Optional default options for all loggers.
|
||||
* @param {Boolean} opt.logInConsole Should all loggers print to console by default ? Default true.
|
||||
* @param {Boolean} opt.logInFile Should all loggers saver log in file by default ? Default true.
|
||||
*
|
||||
* @param {int} opt.contextMediumSize Medium size of the context part of a log message.
|
||||
* Used when calculating indent. Default to 45.
|
||||
* @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 {int} opt.contextMediumSize Medium size of the context part of a log message.
|
||||
* Used when calculating indent. Default to 45.
|
||||
* @param {int} opt.spaceSize Space between context part and log part. Default to 4.
|
||||
* @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.alwaysLocation Always print location (even without file() ). Default false.
|
||||
* @param {Boolean} opt.alwaysTime Always print time (even without time() ). Default false.
|
||||
* @param {Boolean} opt.alwaysDate Always print date (even without date() ). 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.alwaysTime Always print time (even without time() ). Default false.
|
||||
* @param {Boolean} opt.alwaysDate Always print date (even without date() ). Default false.
|
||||
*/
|
||||
var Console2 = function (opt) {
|
||||
|
||||
|
@ -113,7 +141,7 @@
|
|||
contextMediumSize : opt.contextMediumSize || 45,
|
||||
spaceSize : opt.spaceSize || 4,
|
||||
|
||||
color : opt.color || "cyan",
|
||||
colors : opt.color || "cyan",
|
||||
|
||||
alwaysTags : opt.alwaysTags || false,
|
||||
alwaysLocation : opt.alwaysLocation || false,
|
||||
|
@ -297,7 +325,7 @@
|
|||
* You can then use it with console.myNewLogger
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @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.alwaysDate Always print date (even without date() )
|
||||
*/
|
||||
Console2.prototype.addLogger = function (name, color, opt) {
|
||||
Console2.prototype.addLogger = function (name, colors, opt) {
|
||||
|
||||
if (!opt) {
|
||||
opt = {};
|
||||
|
@ -319,7 +347,7 @@
|
|||
}
|
||||
|
||||
opt.name = name;
|
||||
opt.color = color || this.opt.color;
|
||||
opt.colors = colors || this.opt.colors;
|
||||
opt.type = opt.type || opt.name;
|
||||
opt.logInConsole = opt.logInConsole || this.opt.logInConsole;
|
||||
opt.logInFile = opt.logInFile || this.opt.logInFile;
|
||||
|
@ -328,6 +356,7 @@
|
|||
opt.alwaysTime = opt.alwaysTime || this.opt.alwaysTime;
|
||||
opt.alwaysDate = opt.alwaysDate || this.opt.alwaysDate;
|
||||
|
||||
|
||||
this[name] = function () {
|
||||
|
||||
var location = getLocation();
|
||||
|
@ -357,22 +386,13 @@
|
|||
});
|
||||
|
||||
//Emit events
|
||||
this.emit('new', log);
|
||||
this.emit(opt.type || name, log);
|
||||
this.emit('new', log, log.type);
|
||||
this.emit(log.type, log);
|
||||
|
||||
//If the logger should print the message
|
||||
//Print it
|
||||
if (opt.logInConsole) {
|
||||
|
||||
var msg;
|
||||
|
||||
if (typeof colors[opt.color] === 'function') {
|
||||
msg = colors[opt.color](log.message);
|
||||
} else {
|
||||
msg = log.message;
|
||||
}
|
||||
|
||||
global.console.log(msg);
|
||||
global.console.log(applyColors(log.message, opt.colors));
|
||||
}
|
||||
|
||||
this._reset();
|
||||
|
|
Loading…
Add table
Reference in a new issue