Merge pull request #101 from Dignifiedquire/feature-color-pattern

[feature] Add patternColoured Layout.
This commit is contained in:
Gareth Jones 2012-12-02 14:51:27 -08:00
commit 8b42e46071
2 changed files with 37 additions and 26 deletions

View File

@ -24,7 +24,6 @@ var dateFormat = require('./date_format')
, OFF: "grey"
};
function formatLogData(logData) {
var output = ""
, data = Array.isArray(logData) ? logData.slice() : Array.prototype.slice.call(arguments)
@ -58,30 +57,36 @@ function formatLogData(logData) {
return output;
}
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]
};
function colorizeStart(style) {
return style ? '\033[' + styles[style][0] + 'm' : '';
}
function colorizeEnd(style) {
return style ? '\033[' + styles[style][1] + 'm' : '';
}
/**
* 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 style ? '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm' : str;
return colorizeStart(style) + str + colorizeEnd(style);
}
function timestampLevelAndCategory(loggingEvent, colour) {
@ -134,12 +139,13 @@ function messagePassThroughLayout (loggingEvent) {
* - %d date in various formats
* - %% %
* - %n newline
* You can use %[ and %] to define a colored block.
* Takes a pattern string and returns a layout function.
* @author Stephan Strittmatter
*/
function patternLayout (pattern) {
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([cdmnpr%])(\{([^\}]+)\})?|([^%]+)/;
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdmnpr%])(\{([^\}]+)\})?|([^%]+)/;
pattern = pattern || TTCC_CONVERSION_PATTERN;
@ -206,6 +212,12 @@ function patternLayout (pattern) {
case "r":
replacement = "" + loggingEvent.startTime.toLocaleTimeString();
break;
case "[":
replacement = colorizeStart(colours[loggingEvent.level.toString()]);
break;
case "]":
replacement = colorizeEnd(colours[loggingEvent.level.toString()]);
break;
case "%":
replacement = "%";
break;
@ -248,7 +260,6 @@ function patternLayout (pattern) {
};
module.exports = {
basicLayout: basicLayout
, messagePassThroughLayout: messagePassThroughLayout

View File

@ -243,9 +243,9 @@ vows.describe('log4js layouts').addBatch({
test(args, '%-6p', 'DEBUG ');
test(args, '%-8p', 'DEBUG ');
test(args, '%-10p', 'DEBUG ');
},
'%[%r%] should output colored time': function(args) {
test(args, '%[%r%]', '\033[36m14:18:30\033[39m');
}
}
}).export(module);