8000 added masylum's coloured layout function · Web5design/log4js-node@8145e3b · GitHub
[go: up one dir, main page]

Skip to content

Commit 8145e3b

Browse files
author
csausdev
committed
added masylum's coloured layout function
1 parent 04e1446 commit 8145e3b

File tree

3 files changed

+85
-17
lines changed

3 files changed

+85
-17
lines changed

lib/log4js.js

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
5656
loggers = {},
5757
appenders = {},
5858
levels = {
59-
ALL: new Level(Number.MIN_VALUE, "ALL"),
60-
TRACE: new Level(5000, "TRACE"),
61-
DEBUG: new Level(10000, "DEBUG"),
62-
INFO: new Level(20000, "INFO"),
63-
WARN: new Level(30000, "WARN"),
64-
ERROR: new Level(40000, "ERROR"),
65-
FATAL: new Level(50000, "FATAL"),
66-
OFF: new Level(Number.MAX_VALUE, "OFF")
59+
ALL: new Level(Number.MIN_VALUE, "ALL", "grey"),
60+
TRACE: new Level(5000, "TRACE", "blue"),
61+
DEBUG: new Level(10000, "DEBUG", "cyan"),
62+
INFO: new Level(20000, "INFO", "green"),
63+
WARN: new Level(30000, "WARN", "yellow"),
64+
ERROR: new Level(40000, "ERROR", "red"),
65+
FATAL: new Level(50000, "FATAL", "magenta"),
66+
OFF: new Level(Number.MAX_VALUE, "OFF", "grey")
6767
},
6868
appenderMakers = {
6969
"file": function(config) {
@@ -226,9 +226,10 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
226226
}
227227
}
228228

229-
function Level(level, levelStr) {
229+
function Level(level, levelStr, colour) {
230230
this.level = level;
231231
this.levelStr = levelStr;
232+
this.colour = colour;
232233
}
233234

234235
/**
@@ -334,7 +335,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
334335
}
335336

336337
function consoleAppender (layout) {
337-
layout = layout || basicLayout;
338+
layout = layout || colouredLayout;
338339
return function(loggingEvent) {
339340
standardOutput(layout(loggingEvent));
340341
};
@@ -394,6 +395,57 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
394395
return output;
395396
}
396397

398+
/**
399+
* Taken from masylum's fork (https://github.com/masylum/log4js-node)
400+
*/
401+
function colorize (str, style) {
402+
var styles = {
403+
//styles
404+
'bold' : [1, 22],
405+
'italic' : [3, 23],
406+
'underline' : [4, 24],
407+
'inverse' : [7, 27],
408+
//grayscale
409+
'white' : [37, 39],
410+
'grey' : [90, 39],
411+
'black' : [90, 39],
412+
//colors
413+
'blue' : [34, 39],
414+
'cyan' : [36, 39],
415+
'green' : [32, 39],
416+
'magenta' : [35, 39],
417+
'red' : [31, 39],
418+
'yellow' : [33, 39]
419+
};
420+
return '\033[' + styles[style][0] + 'm' + str +
421+
'\033[' + styles[style][1] + 'm';
422+
}
423+
424+
/**
425+
* colouredLayout - taken from masylum's fork.
426+
* same as basicLayout, but with colours.
427+
*/
428+
function colouredLayout (loggingEvent) {
429+
var timestampLevelAndCategory = colorize('[' + loggingEvent.startTime.toFormattedString() + '] ', 'grey');
430+
timestampLevelAndCategory += colorize(
431+
'[' + loggingEvent.level.toString() + '] ', loggingEvent.level.colour
432+
);
433+
timestampLevelAndCategory += colorize(loggingEvent.categoryName + ' - ', 'grey');
434+
435+
var output = timestampLevelAndCategory + loggingEvent.message;
436+
437+
if (loggingEvent.exception) {
438+
output += '\n'
439+
output += timestampLevelAndCategory;
440+
if (loggingEvent.exception.stack) {
441+
output += loggingEvent.exception.stack;
442+
} else {
443+
output += loggingEvent.exception.name + ': '+loggingEvent.exception.message;
444+
}
445+
}
446+
return output;
447+
}
448+
397449
function messagePassThroughLayout (loggingEvent) {
398450
return loggingEvent.message;
399451
}
@@ -533,7 +585,9 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
533585

534586
basicLayout: basicLayout,
535587
messagePassThroughLayout: messagePassThroughLayout,
536-
patternLayout: patternLayout
588+
patternLayout: patternLayout,
589+
colouredLayout: colouredLayout,
590+
coloredLayout: colouredLayout
537591
};
538592
}
539593

lib/log4js.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{
22
"appenders": [
33
{
4-
"type": "console",
5-
"layout": {
6-
"type": "basic"
7-
}
4+
"type": "console"
85
}
96
]
107
}

test/logging.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,24 @@ vows.describe('log4js').addBatch({
188188
var message = args[1];
189189
assert.equal(message, 'this is a test');
190190
}
191+
},
192+
193+
'colouredLayout': {
194+
topic: function() {
195+
return require('../lib/log4js')().colouredLayout;
196+
},
197+
198+
'should apply level colour codes to output': function(layout) {
199+
var output = layout({
200+
message: "nonsense",
201+
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
202+
categoryName: "cheese",
203+
level: {
204+
colour: "green",
205+
toString: function() { return "ERROR"; }
206+
}
207+
});
208+
assert.equal(output, '\033[90m[2010-12-05 14:18:30.045] \033[39m\033[32m[ERROR] \033[39m\033[90mcheese - \033[39mnonsense');
209+
}
191210
}
192-
193-
194211
}).export(module);

0 commit comments

Comments
 (0)
0