Changed layouts to use util.format instead of my own implementation

This commit is contained in:
Gareth Jones 2013-01-11 15:35:00 +11:00
parent ec2f8fec3b
commit 0dbc4921a3
2 changed files with 25 additions and 44 deletions

View File

@ -24,37 +24,19 @@ var dateFormat = require('./date_format')
, OFF: "grey" , OFF: "grey"
}; };
function formatLogData(logData) { function wrapErrorsWithInspect(items) {
var output = "" return items.map(function(item) {
, data = Array.isArray(logData) ? logData.slice() : Array.prototype.slice.call(arguments) if ((item instanceof Error) && item.stack) {
, format = data.shift(); return { inspect: function() { return util.format(item) + '\n' + item.stack; } };
if (typeof format === "string") {
output = format.replace(replacementRegExp, function(match) {
switch (match) {
case "%s": return new String(data.shift());
case "%d": return new Number(data.shift());
case "%j": return JSON.stringify(data.shift());
default:
return match;
};
});
} else { } else {
//put it back, it's not a format string return item;
data.unshift(format);
} }
});
}
data.forEach(function (item) { function formatLogData(logData) {
if (output) { var data = Array.isArray(logData) ? logData : Array.prototype.slice.call(arguments);
output += ' '; return util.format.apply(util, wrapErrorsWithInspect(data));
}
output += util.inspect(item);
if (item && item.stack) {
output += "\n" + item.stack;
}
});
return output;
} }
var styles = { var styles = {

View File

@ -64,7 +64,7 @@ vows.describe('log4js layouts').addBatch({
colour: "green" colour: "green"
, toString: function() { return "ERROR"; } , toString: function() { return "ERROR"; }
} }
}), "thing 1 'cheese'"); }), "thing 1 cheese");
}, },
'should output the first item even if it is not a string': function(layout) { 'should output the first item even if it is not a string': function(layout) {
assert.equal(layout({ assert.equal(layout({
@ -89,21 +89,20 @@ vows.describe('log4js layouts').addBatch({
}).match(/Error\s+at Object\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s+at runTest/) }).match(/Error\s+at Object\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s+at runTest/)
, 'regexp did not return a match'); , 'regexp did not return a match');
}, },
'with passed augmented errors': 'with passed augmented errors': {
{ topic: topic: function(layout){
function(layout){ var e = new Error("My Unique Error Message");
var e = new Error("My Unique Error Message"); e.augmented = "My Unique attribute value"
e.augmented = "My Unique attribute value" e.augObj = { at1: "at2" }
e.augObj = { at1: "at2" } return layout({
return layout({ data: [ e ]
data: [ e ] , startTime: new Date(2010, 11, 5, 14, 18, 30, 45)
, startTime: new Date(2010, 11, 5, 14, 18, 30, 45) , categoryName: "cheese"
, categoryName: "cheese" , level: {
, level: { colour: "green"
colour: "green" , toString: function() { return "ERROR"; }
, toString: function() { return "ERROR"; } }
} });
});
}, },
'should print error the contained error message': function(layoutOutput) { 'should print error the contained error message': function(layoutOutput) {
var m = layoutOutput.match(/\{ \[Error: My Unique Error Message\]/); var m = layoutOutput.match(/\{ \[Error: My Unique Error Message\]/);