added masylum's coloured layout function
This commit is contained in:
parent
04e1446c42
commit
8145e3b08f
@ -56,14 +56,14 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
loggers = {},
|
loggers = {},
|
||||||
appenders = {},
|
appenders = {},
|
||||||
levels = {
|
levels = {
|
||||||
ALL: new Level(Number.MIN_VALUE, "ALL"),
|
ALL: new Level(Number.MIN_VALUE, "ALL", "grey"),
|
||||||
TRACE: new Level(5000, "TRACE"),
|
TRACE: new Level(5000, "TRACE", "blue"),
|
||||||
DEBUG: new Level(10000, "DEBUG"),
|
DEBUG: new Level(10000, "DEBUG", "cyan"),
|
||||||
INFO: new Level(20000, "INFO"),
|
INFO: new Level(20000, "INFO", "green"),
|
||||||
WARN: new Level(30000, "WARN"),
|
WARN: new Level(30000, "WARN", "yellow"),
|
||||||
ERROR: new Level(40000, "ERROR"),
|
ERROR: new Level(40000, "ERROR", "red"),
|
||||||
FATAL: new Level(50000, "FATAL"),
|
FATAL: new Level(50000, "FATAL", "magenta"),
|
||||||
OFF: new Level(Number.MAX_VALUE, "OFF")
|
OFF: new Level(Number.MAX_VALUE, "OFF", "grey")
|
||||||
},
|
},
|
||||||
appenderMakers = {
|
appenderMakers = {
|
||||||
"file": function(config) {
|
"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.level = level;
|
||||||
this.levelStr = levelStr;
|
this.levelStr = levelStr;
|
||||||
|
this.colour = colour;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -334,7 +335,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function consoleAppender (layout) {
|
function consoleAppender (layout) {
|
||||||
layout = layout || basicLayout;
|
layout = layout || colouredLayout;
|
||||||
return function(loggingEvent) {
|
return function(loggingEvent) {
|
||||||
standardOutput(layout(loggingEvent));
|
standardOutput(layout(loggingEvent));
|
||||||
};
|
};
|
||||||
@ -394,6 +395,57 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
return output;
|
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) {
|
function messagePassThroughLayout (loggingEvent) {
|
||||||
return loggingEvent.message;
|
return loggingEvent.message;
|
||||||
}
|
}
|
||||||
@ -533,7 +585,9 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
|
|
||||||
basicLayout: basicLayout,
|
basicLayout: basicLayout,
|
||||||
messagePassThroughLayout: messagePassThroughLayout,
|
messagePassThroughLayout: messagePassThroughLayout,
|
||||||
patternLayout: patternLayout
|
patternLayout: patternLayout,
|
||||||
|
colouredLayout: colouredLayout,
|
||||||
|
coloredLayout: colouredLayout
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
{
|
{
|
||||||
"appenders": [
|
"appenders": [
|
||||||
{
|
{
|
||||||
"type": "console",
|
"type": "console"
|
||||||
"layout": {
|
|
||||||
"type": "basic"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -188,7 +188,24 @@ vows.describe('log4js').addBatch({
|
|||||||
var message = args[1];
|
var message = args[1];
|
||||||
assert.equal(message, 'this is a test');
|
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);
|
}).export(module);
|
||||||
|
Loading…
Reference in New Issue
Block a user