From 30d39b8232560c2f1ceddc7ed6ea3908aa593ec7 Mon Sep 17 00:00:00 2001 From: Guillaume Wuip Date: Sun, 26 Oct 2014 16:17:26 +0100 Subject: [PATCH] Add custom colors options for context part --- lib/console2.js | 59 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/lib/console2.js b/lib/console2.js index 299051e..7b13f5a 100644 --- a/lib/console2.js +++ b/lib/console2.js @@ -184,7 +184,13 @@ * @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 {Array|String} opt.tagsColors Default colors output for tags. Default undefined. + * @param {Array|String} opt.timeColors Default colors output for time. Default undefined. + * @param {Array|String} opt.dateColors Default colors output for date. Default undefined. + * @param {Array|String} opt.fileColors Default colors output for filename. Default undefined. + * @param {Array|String} opt.lineColors Default colors output for line number. Default undefined. * * @param {Boolean} opt.alwaysTags Always print tags (even without tag() ). Default false. * @param {Boolean} opt.alwaysLocation Always print location (even without file() ). Default false. @@ -210,7 +216,12 @@ contextMediumSize : opt.contextMediumSize || 45, spaceSize : opt.spaceSize || 4, - colors : opt.colors || "cyan", + colors : opt.colors || "cyan", + tagsColors : opt.tagsColors, + timeColors : opt.timeColors, + dateColors : opt.dateColors, + lineColors : opt.lineColors, + fileColors : opt.fileColors, alwaysTags : opt.alwaysTags === true, alwaysLocation : opt.alwaysLocation === true, @@ -357,25 +368,25 @@ if (opt.tags && log.context.tags) { var tags = buildTags(log.context.tags); - result += tags.msg + space; + result += applyColors(tags.msg, log.opt.tagsColors) + space; length += tags.msgLength + space.length; } if (opt.location && log.context.location.filename && log.context.location.line) { - var infos = buildFileInfos(log.context.location); + var infos = buildFileInfos(log.context.location, log.opt.fileColors, log.opt.lineColors); result += infos.msg + space; length += infos.msgLength + space.length; } if (opt.time && log.context.time) { var time = buildTime(log.context.time); - result += time.msg + space; + result += applyColors(time.msg, log.opt.timeColors) + space; length += time.msgLength + space.length; } if (opt.date && log.context.time) { var date = buildDate(log.context.time); - result += date.msg + space; + result += applyColors(date.msg, log.opt.dateColors) + space; length += date.msgLength + space.length; } @@ -448,17 +459,25 @@ * Create a new logger * You can then use it with console.myNewLogger * - * @param {String} name The name of the logger. - * @param {String} colors Optional. Colors of the console output. Default cyan. - * See text colors from https://github.com/Marak/colors.js + * @param {String} name The name of the logger. + * @param {Array|String} colors Optional colorsjs colors of the console output. + * Override constructor default. + * See text colors from https://github.com/Marak/colors.js * - * @param {Object} opt Optional options object. @see Console2 opt for default values. - * @param {Boolean} opt.logInConsole Should the logger print to the console ? - * @param {Boolean} opt.logInFile If the log should be save in file. - * @param {Boolean} opt.alwaysTags Always print tags (even without tag() ) - * @param {Boolean} opt.alwaysLocation Always print location (even without file() ) - * @param {Boolean} opt.alwaysTime Always print time (even without time() ) - * @param {Boolean} opt.alwaysDate Always print date (even without date() ) + * @param {Object} opt Optional options object. @see Console2 opt for default values. + * + * @param {Array|String} opt.tagsColors Default colors output for tags. Default undefined. + * @param {Array|String} opt.timeColors Default colors output for time. Default undefined. + * @param {Array|String} opt.dateColors Default colors output for date. Default undefined. + * @param {Array|String} opt.fileColors Default colors output for filename. Default undefined. + * @param {Array|String} opt.lineColors Default colors output for line number. Default undefined. + * + * @param {Boolean} opt.logInConsole Should the logger print to the console ? + * @param {Boolean} opt.logInFile If the log should be save in file. + * @param {Boolean} opt.alwaysTags Always print tags (even without tag() ) + * @param {Boolean} opt.alwaysLocation Always print location (even without file() ) + * @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, colors, opt) { @@ -471,8 +490,15 @@ } opt.name = name; - opt.colors = colors || this.opt.colors; opt.type = opt.type || opt.name; + + opt.colors = colors || this.opt.colors; + opt.tagsColors = opt.tagsColors || this.opt.tagsColors; + opt.timeColors = opt.timeColors || this.opt.timeColors; + opt.dateColors = opt.dateColors || this.opt.dateColors; + opt.fileColors = opt.fileColors || this.opt.fileColors; + opt.lineColors = opt.lineColors || this.opt.lineColors; + opt.logInConsole = opt.logInConsole || this.opt.logInConsole; opt.logInFile = opt.logInFile || this.opt.logInFile; opt.alwaysTags = opt.alwaysTags || this.opt.alwaysTags; @@ -480,7 +506,6 @@ opt.alwaysTime = opt.alwaysTime || this.opt.alwaysTime; opt.alwaysDate = opt.alwaysDate || this.opt.alwaysDate; - this[name] = function () { var location = getLocation();