diff --git a/lib/log4js.js b/lib/log4js.js index c5424db..85b6714 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -56,14 +56,14 @@ module.exports = function (fileSystem, standardOutput, configPaths) { loggers = {}, appenders = {}, levels = { - ALL: new Level(Number.MIN_VALUE, "ALL"), - TRACE: new Level(5000, "TRACE"), - DEBUG: new Level(10000, "DEBUG"), - INFO: new Level(20000, "INFO"), - WARN: new Level(30000, "WARN"), - ERROR: new Level(40000, "ERROR"), - FATAL: new Level(50000, "FATAL"), - OFF: new Level(Number.MAX_VALUE, "OFF") + ALL: new Level(Number.MIN_VALUE, "ALL", "grey"), + TRACE: new Level(5000, "TRACE", "blue"), + DEBUG: new Level(10000, "DEBUG", "cyan"), + INFO: new Level(20000, "INFO", "green"), + WARN: new Level(30000, "WARN", "yellow"), + ERROR: new Level(40000, "ERROR", "red"), + FATAL: new Level(50000, "FATAL", "magenta"), + OFF: new Level(Number.MAX_VALUE, "OFF", "grey") }, appenderMakers = { "file": function(config) { @@ -226,9 +226,10 @@ module.exports = function (fileSystem, standardOutput, configPaths) { } } - function Level(level, levelStr) { + function Level(level, levelStr, colour) { this.level = level; this.levelStr = levelStr; + this.colour = colour; } /** @@ -334,7 +335,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) { } function consoleAppender (layout) { - layout = layout || basicLayout; + layout = layout || colouredLayout; return function(loggingEvent) { standardOutput(layout(loggingEvent)); }; @@ -394,6 +395,57 @@ module.exports = function (fileSystem, standardOutput, configPaths) { return output; } + /** + * Taken from masylum's fork (https://github.com/masylum/log4js-node) + */ + function colorize (str, style) { + var styles = { + //styles + 'bold' : [1, 22], + 'italic' : [3, 23], + 'underline' : [4, 24], + 'inverse' : [7, 27], + //grayscale + 'white' : [37, 39], + 'grey' : [90, 39], + 'black' : [90, 39], + //colors + 'blue' : [34, 39], + 'cyan' : [36, 39], + 'green' : [32, 39], + 'magenta' : [35, 39], + 'red' : [31, 39], + 'yellow' : [33, 39] + }; + return '\033[' + styles[style][0] + 'm' + str + + '\033[' + styles[style][1] + 'm'; + } + + /** + * colouredLayout - taken from masylum's fork. + * same as basicLayout, but with colours. + */ + function colouredLayout (loggingEvent) { + var timestampLevelAndCategory = colorize('[' + loggingEvent.startTime.toFormattedString() + '] ', 'grey'); + timestampLevelAndCategory += colorize( + '[' + loggingEvent.level.toString() + '] ', loggingEvent.level.colour + ); + timestampLevelAndCategory += colorize(loggingEvent.categoryName + ' - ', 'grey'); + + var output = timestampLevelAndCategory + loggingEvent.message; + + if (loggingEvent.exception) { + output += '\n' + output += timestampLevelAndCategory; + if (loggingEvent.exception.stack) { + output += loggingEvent.exception.stack; + } else { + output += loggingEvent.exception.name + ': '+loggingEvent.exception.message; + } + } + return output; + } + function messagePassThroughLayout (loggingEvent) { return loggingEvent.message; } @@ -533,7 +585,9 @@ module.exports = function (fileSystem, standardOutput, configPaths) { basicLayout: basicLayout, messagePassThroughLayout: messagePassThroughLayout, - patternLayout: patternLayout + patternLayout: patternLayout, + colouredLayout: colouredLayout, + coloredLayout: colouredLayout }; } diff --git a/lib/log4js.json b/lib/log4js.json index fdd836f..6c2998c 100644 --- a/lib/log4js.json +++ b/lib/log4js.json @@ -1,10 +1,7 @@ { "appenders": [ { - "type": "console", - "layout": { - "type": "basic" - } + "type": "console" } ] } \ No newline at end of file diff --git a/test/logging.js b/test/logging.js index fc9e471..215c73e 100644 --- a/test/logging.js +++ b/test/logging.js @@ -188,7 +188,24 @@ vows.describe('log4js').addBatch({ var message = args[1]; assert.equal(message, 'this is a test'); } + }, + + 'colouredLayout': { + topic: function() { + return require('../lib/log4js')().colouredLayout; + }, + + 'should apply level colour codes to output': function(layout) { + var output = layout({ + message: "nonsense", + startTime: new Date(2010, 11, 5, 14, 18, 30, 45), + categoryName: "cheese", + level: { + colour: "green", + toString: function() { return "ERROR"; } + } + }); + assert.equal(output, '\033[90m[2010-12-05 14:18:30.045] \033[39m\033[32m[ERROR] \033[39m\033[90mcheese - \033[39mnonsense'); + } } - - }).export(module);