Fix for stack traces appearing twice in node v6 - issue #389

This commit is contained in:
Gareth Jones 2016-06-15 09:35:08 +10:00
parent 60c48a58d7
commit 08315bbfb9
3 changed files with 31 additions and 11 deletions

View File

@ -1,6 +1,7 @@
language: node_js
sudo: false
node_js:
- "6"
- "5"
- "4"
- "0.12"

View File

@ -3,6 +3,7 @@ var dateFormat = require('./date_format')
, os = require('os')
, eol = os.EOL || '\n'
, util = require('util')
, semver = require('semver')
, replacementRegExp = /%[sdj]/g
, layoutMakers = {
"messagePassThrough": function() { return messagePassThroughLayout; },
@ -28,7 +29,13 @@ var dateFormat = require('./date_format')
function wrapErrorsWithInspect(items) {
return items.map(function(item) {
if ((item instanceof Error) && item.stack) {
return { inspect: function() { return util.format(item) + '\n' + item.stack; } };
return { inspect: function() {
if (semver.satisfies(process.version, '>=6')) {
return util.format(item);
} else {
return util.format(item) + '\n' + item.stack;
}
} };
} else {
return item;
}

View File

@ -2,6 +2,7 @@
var vows = require('vows')
, assert = require('assert')
, os = require('os')
, semver = require('semver')
, EOL = os.EOL || '\n';
//used for patternLayout tests.
@ -108,7 +109,7 @@ vows.describe('log4js layouts').addBatch({
});
},
'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/);
assert.isArray(m);
},
'should print error augmented string attributes': function(layoutOutput) {
@ -142,7 +143,7 @@ vows.describe('log4js layouts').addBatch({
assert.equal(layout(event), "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test");
},
'should output a stacktrace, message if the event has an error attached': function(args) {
var layout = args[0], event = args[1], output, lines,
var i, layout = args[0], event = args[1], output, lines,
error = new Error("Some made-up error"),
stack = error.stack.split(/\n/);
@ -150,15 +151,26 @@ vows.describe('log4js layouts').addBatch({
output = layout(event);
lines = output.split(/\n/);
assert.equal(lines.length - 1, stack.length);
assert.equal(
lines[0],
"[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test [Error: Some made-up error]"
);
for (var i = 1; i < stack.length; i++) {
assert.equal(lines[i+2], stack[i+1]);
if (semver.satisfies(process.version, '>=6')) {
assert.equal(lines.length, stack.length);
assert.equal(
lines[0],
"[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test Error: Some made-up error"
);
for (i = 1; i < stack.length; i++) {
assert.equal(lines[i], stack[i]);
}
} else {
assert.equal(lines.length - 1, stack.length);
assert.equal(
lines[0],
"[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test [Error: Some made-up error]"
);
for (i = 1; i < stack.length; i++) {
assert.equal(lines[i+2], stack[i+1]);
}
}
},
'should output any extra data in the log event as util.inspect strings': function(args) {
var layout = args[0], event = args[1], output, lines;