Skip to content

Commit

Permalink
added masylum's coloured layout function
Browse files Browse the repository at this point in the history
  • Loading branch information
csausdev committed Apr 6, 2011
1 parent 838f0c8 commit 80e3ed7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 17 deletions.
76 changes: 65 additions & 11 deletions lib/log4js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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));
};
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -533,7 +585,9 @@ module.exports = function (fileSystem, standardOutput, configPaths) {

basicLayout: basicLayout,
messagePassThroughLayout: messagePassThroughLayout,
patternLayout: patternLayout
patternLayout: patternLayout,
colouredLayout: colouredLayout,
coloredLayout: colouredLayout
};
}

Expand Down
5 changes: 1 addition & 4 deletions lib/log4js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"appenders": [
{
"type": "console",
"layout": {
"type": "basic"
}
"type": "console"
}
]
}
21 changes: 19 additions & 2 deletions test/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 80e3ed7

Please sign in to comment.