Skip to content

Commit

Permalink
Added the options to colorize only the level, only the message or all.
Browse files Browse the repository at this point in the history
Default behavior is kept. Using true will only colorize the level and false will not colorize anything.
  • Loading branch information
MichielDeMey authored and indexzero committed Jan 6, 2015
1 parent b420477 commit 72273b1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
11 changes: 11 additions & 0 deletions examples/color-message.js
Original file line number Diff line number Diff line change
@@ -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.');
9 changes: 7 additions & 2 deletions lib/winston/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions lib/winston/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand Down

0 comments on commit 72273b1

Please sign in to comment.