diff --git a/lib/jog.js b/lib/jog.js index cb623ed..b71dbbf 100644 --- a/lib/jog.js +++ b/lib/jog.js @@ -99,6 +99,15 @@ Jog.prototype.write = function(level, type, attrs){ }); } + // check for Error + var err = attrs.error || attrs + if (err instanceof Error) { + attrs.error = { + stack: err.stack, + message: err.message + }; + } + // add it to the store this.store.add(attrs); return this; @@ -137,4 +146,4 @@ exports.levels.forEach(function(level){ Jog.prototype[level] = function(type, attrs){ this.write(level, type, attrs); }; -}) \ No newline at end of file +}) diff --git a/test/jog.js b/test/jog.js index 3a64003..aa0f6dc 100644 --- a/test/jog.js +++ b/test/jog.js @@ -30,6 +30,22 @@ describe('Jog', function(){ var log = new Jog(store); log.info('something happened'); }) + + it('should handle error instances', function (done){ + var err = new Error('BOOM') + + var store = { + add: function(obj){ + obj.error.stack.should.equal(err.stack); + obj.error.message.should.equal(err.message); + } + }; + + var log = new Jog(store); + log.error('something happened', { error: err }); + log.error('even worse', err); + done(); + }) }) describe('#ns(obj)', function(){ @@ -52,4 +68,4 @@ describe('Jog', function(){ log.info('something happened'); }) }) -}) \ No newline at end of file +})