Merge pull request #168 from matomesc/master_matomesc

Use the correct error serializer
This commit is contained in:
Trent Mick 2014-09-27 21:02:15 -07:00
commit bc49f14cfa
2 changed files with 42 additions and 2 deletions

View file

@ -828,13 +828,16 @@ function mkLogEmitter(minLevel) {
var excludeFields; var excludeFields;
if (args[0] instanceof Error) { if (args[0] instanceof Error) {
// `log.<level>(err, ...)` // `log.<level>(err, ...)`
fields = {err: errSerializer(args[0])}; fields = {
// if a serializer is defined for err use it else fallback on the default serializer
err: log.serializers.err ? log.serializers.err(args[0]) : Logger.stdSerializers.err(args[0])
};
excludeFields = {err: true}; excludeFields = {err: true};
if (args.length === 1) { if (args.length === 1) {
msgArgs = [fields.err.message]; msgArgs = [fields.err.message];
} else { } else {
msgArgs = Array.prototype.slice.call(args, 1); msgArgs = Array.prototype.slice.call(args, 1);
} }
} else if (typeof (args[0]) !== 'object' && args[0] !== null || } else if (typeof (args[0]) !== 'object' && args[0] !== null ||
Array.isArray(args[0])) { Array.isArray(args[0])) {
// `log.<level>(msg, ...)` // `log.<level>(msg, ...)`

View file

@ -181,6 +181,43 @@ test('err serializer', function (t) {
t.end(); t.end();
}); });
test('err serializer: custom serializer', function (t) {
var records = [];
function customSerializer(err) {
return {
message: err.message,
name: err.name,
stack: err.stack,
beep: err.beep
};
}
var log = bunyan.createLogger({
name: 'serializer-test',
streams: [
{
stream: new CapturingStream(records),
type: 'raw'
}
],
serializers: {
err: customSerializer
}
});
var e1 = new Error('message1');
e1.beep = 'bop';
var e2 = new Error('message2');
var errs = [e1, e2];
for (var i = 0; i < errs.length; i++) {
log.info(errs[i]);
t.equal(records[i].err.message, errs[i].message);
t.equal(records[i].err.beep, errs[i].beep);
}
t.end();
});
test('err serializer: long stack', function (t) { test('err serializer: long stack', function (t) {
var records = []; var records = [];