merged util.format branch (fixes a lint error and simplifies the code)
This commit is contained in:
commit
41ddf5eea7
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,5 +2,5 @@
|
||||
*.log??
|
||||
build
|
||||
node_modules
|
||||
|
||||
.bob/
|
||||
test/streams/test-rolling-file-stream*
|
||||
|
@ -26,40 +26,19 @@ var dateFormat = require('./date_format')
|
||||
OFF: "grey"
|
||||
};
|
||||
|
||||
function formatLogData(logData) {
|
||||
var output = ""
|
||||
, data = Array.isArray(logData) ? logData.slice() : Array.prototype.slice.call(arguments)
|
||||
, format = data.shift();
|
||||
|
||||
if (typeof format === "string") {
|
||||
output = format.replace(replacementRegExp, function(match) {
|
||||
switch (match) {
|
||||
case "%s":
|
||||
return data.shift().toString();
|
||||
case "%d":
|
||||
return new Number(data.shift());
|
||||
case "%j":
|
||||
return JSON.stringify(data.shift());
|
||||
default:
|
||||
return match;
|
||||
}
|
||||
});
|
||||
function wrapErrorsWithInspect(items) {
|
||||
return items.map(function(item) {
|
||||
if ((item instanceof Error) && item.stack) {
|
||||
return { inspect: function() { return util.format(item) + '\n' + item.stack; } };
|
||||
} else {
|
||||
//put it back, it's not a format string
|
||||
data.unshift(format);
|
||||
}
|
||||
|
||||
data.forEach(function (item) {
|
||||
if (output) {
|
||||
output += ' ';
|
||||
}
|
||||
output += util.inspect(item);
|
||||
if (item && item.stack) {
|
||||
output += "\n" + item.stack;
|
||||
return item;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return output;
|
||||
function formatLogData(logData) {
|
||||
var data = Array.isArray(logData) ? logData : Array.prototype.slice.call(arguments);
|
||||
return util.format.apply(util, wrapErrorsWithInspect(data));
|
||||
}
|
||||
|
||||
var styles = {
|
||||
|
@ -28,7 +28,6 @@ vows.describe('log4js layouts').addBatch({
|
||||
});
|
||||
assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mnonsense');
|
||||
},
|
||||
|
||||
'should support the console.log format for the message': function(layout) {
|
||||
var output = layout({
|
||||
data: ["thing %d", 2],
|
||||
@ -66,7 +65,7 @@ vows.describe('log4js layouts').addBatch({
|
||||
colour: "green",
|
||||
toString: function() { return "ERROR"; }
|
||||
}
|
||||
}), "thing 1 'cheese'");
|
||||
}), "thing 1 cheese");
|
||||
},
|
||||
'should output the first item even if it is not a string': function(layout) {
|
||||
assert.equal(layout({
|
||||
|
Loading…
Reference in New Issue
Block a user