[feature] Add color option to pattern layout.
Based on #90 this implements the possibillity to add the color codes according to the log level via %[ and %].
This commit is contained in:
parent
a9307fd6da
commit
4a7a90ed53
@ -24,7 +24,6 @@ var dateFormat = require('./date_format')
|
|||||||
, OFF: "grey"
|
, OFF: "grey"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function formatLogData(logData) {
|
function formatLogData(logData) {
|
||||||
var output = ""
|
var output = ""
|
||||||
, data = Array.isArray(logData) ? logData.slice() : Array.prototype.slice.call(arguments)
|
, data = Array.isArray(logData) ? logData.slice() : Array.prototype.slice.call(arguments)
|
||||||
@ -58,11 +57,7 @@ function formatLogData(logData) {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
var styles = {
|
||||||
* Taken from masylum's fork (https://github.com/masylum/log4js-node)
|
|
||||||
*/
|
|
||||||
function colorize (str, style) {
|
|
||||||
var styles = {
|
|
||||||
//styles
|
//styles
|
||||||
'bold' : [1, 22],
|
'bold' : [1, 22],
|
||||||
'italic' : [3, 23],
|
'italic' : [3, 23],
|
||||||
@ -79,9 +74,19 @@ function colorize (str, style) {
|
|||||||
'magenta' : [35, 39],
|
'magenta' : [35, 39],
|
||||||
'red' : [31, 39],
|
'red' : [31, 39],
|
||||||
'yellow' : [33, 39]
|
'yellow' : [33, 39]
|
||||||
};
|
};
|
||||||
return style ? '\033[' + styles[style][0] + 'm' + str +
|
|
||||||
'\033[' + styles[style][1] + 'm' : str;
|
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) {
|
||||||
|
return colorizeStart(style) + str + colorizeEnd(style);
|
||||||
}
|
}
|
||||||
|
|
||||||
function timestampLevelAndCategory(loggingEvent, colour) {
|
function timestampLevelAndCategory(loggingEvent, colour) {
|
||||||
@ -134,12 +139,13 @@ function messagePassThroughLayout (loggingEvent) {
|
|||||||
* - %d date in various formats
|
* - %d date in various formats
|
||||||
* - %% %
|
* - %% %
|
||||||
* - %n newline
|
* - %n newline
|
||||||
|
* You can use %[ and %] to define a colored block.
|
||||||
* Takes a pattern string and returns a layout function.
|
* Takes a pattern string and returns a layout function.
|
||||||
* @author Stephan Strittmatter
|
* @author Stephan Strittmatter
|
||||||
*/
|
*/
|
||||||
function patternLayout (pattern) {
|
function patternLayout (pattern) {
|
||||||
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
|
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;
|
pattern = pattern || TTCC_CONVERSION_PATTERN;
|
||||||
|
|
||||||
@ -206,6 +212,12 @@ function patternLayout (pattern) {
|
|||||||
case "r":
|
case "r":
|
||||||
replacement = "" + loggingEvent.startTime.toLocaleTimeString();
|
replacement = "" + loggingEvent.startTime.toLocaleTimeString();
|
||||||
break;
|
break;
|
||||||
|
case "[":
|
||||||
|
replacement = colorizeStart(colours[loggingEvent.level.toString()]);
|
||||||
|
break;
|
||||||
|
case "]":
|
||||||
|
replacement = colorizeEnd(colours[loggingEvent.level.toString()]);
|
||||||
|
break;
|
||||||
case "%":
|
case "%":
|
||||||
replacement = "%";
|
replacement = "%";
|
||||||
break;
|
break;
|
||||||
@ -248,7 +260,6 @@ function patternLayout (pattern) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
basicLayout: basicLayout
|
basicLayout: basicLayout
|
||||||
, messagePassThroughLayout: messagePassThroughLayout
|
, messagePassThroughLayout: messagePassThroughLayout
|
||||||
|
@ -243,9 +243,9 @@ vows.describe('log4js layouts').addBatch({
|
|||||||
test(args, '%-6p', 'DEBUG ');
|
test(args, '%-6p', 'DEBUG ');
|
||||||
test(args, '%-8p', 'DEBUG ');
|
test(args, '%-8p', 'DEBUG ');
|
||||||
test(args, '%-10p', 'DEBUG ');
|
test(args, '%-10p', 'DEBUG ');
|
||||||
|
},
|
||||||
|
'%[%r%] should output colored time': function(args) {
|
||||||
|
test(args, '%[%r%]', '\033[36m14:18:30\033[39m');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}).export(module);
|
}).export(module);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user