From 72273b15316d77f6e77d9977b23ce19277941519 Mon Sep 17 00:00:00 2001 From: Michiel De Mey Date: Sat, 8 Mar 2014 22:12:11 -0800 Subject: [PATCH] Added the options to colorize only the level, only the message or all. Default behavior is kept. Using true will only colorize the level and false will not colorize anything. --- examples/color-message.js | 11 +++++++++++ lib/winston/common.js | 9 +++++++-- lib/winston/config.js | 6 ++++-- 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 examples/color-message.js diff --git a/examples/color-message.js b/examples/color-message.js new file mode 100644 index 000000000..1622575a8 --- /dev/null +++ b/examples/color-message.js @@ -0,0 +1,11 @@ +var winston = require('../lib/winston'); + +var logger = module.exports = new (winston.Logger)({ + transports: [ + new (winston.transports.Console)({ + colorize: 'all' + }) + ] +}); + +logger.log('info', 'This is an information message.'); \ No newline at end of file diff --git a/lib/winston/common.js b/lib/winston/common.js index 45b12512a..37ef3fb89 100644 --- a/lib/winston/common.js +++ b/lib/winston/common.js @@ -190,10 +190,15 @@ exports.log = function (options) { } output = timestamp ? timestamp + ' - ' : ''; - output += options.colorize ? config.colorize(options.level) : options.level; + output += options.colorize === 'all' || options.colorize === 'level' || options.colorize === true + ? config.colorize(options.level) + : options.level; + output += ': '; output += options.label ? ('[' + options.label + '] ') : ''; - output += options.message; + output += options.colorize === 'all' || options.colorize === 'message' + ? config.colorize(options.level, options.message) + : options.message; if (meta !== null && meta !== undefined) { if (meta && meta instanceof Error && meta.stack) { diff --git a/lib/winston/config.js b/lib/winston/config.js index 346d3ea86..600cb8911 100644 --- a/lib/winston/config.js +++ b/lib/winston/config.js @@ -15,8 +15,10 @@ config.addColors = function (colors) { mixin(allColors, colors); }; -config.colorize = function (level) { - var colorized = level; +config.colorize = function (level, message) { + if (typeof message === 'undefined') message = level; + + var colorized = message; if (allColors[level] instanceof Array) { for (var i = 0, l = allColors[level].length; i < l; ++i) { colorized = colorized[allColors[level][i]];