use the correct error serializer

This commit is contained in:
Mihai Tomescu 2014-09-13 20:12:09 -04:00
parent cb703ef474
commit 576e500b11
2 changed files with 44 additions and 2 deletions

View file

@ -793,13 +793,16 @@ function mkLogEmitter(minLevel) {
var excludeFields;
if (args[0] instanceof Error) {
// `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};
if (args.length === 1) {
msgArgs = [fields.err.message];
} else {
msgArgs = Array.prototype.slice.call(args, 1);
}
}
} else if (typeof (args[0]) !== 'object' && args[0] !== null ||
Array.isArray(args[0])) {
// `log.<level>(msg, ...)`

View file

@ -181,6 +181,45 @@ test('err serializer', function (t) {
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) {
var records = [];