Skip to content

Commit

Permalink
Merge pull request #18 from mcollina/errSerializer
Browse files Browse the repository at this point in the history
Added the error serializer
  • Loading branch information
David Mark Clements committed Mar 31, 2016
2 parents 8603c35 + 0197b41 commit b26d617
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Into this:
* <a href="#trace"><code>logger.<b>trace()</b></code></a>
* <a href="#reqSerializer"><code>pino.stdSerializers.<b>req</b></code></a>
* <a href="#resSerializer"><code>pino.stdSerializers.<b>res</b></code></a>
* <a href="#errSerializer"><code>pino.stdSerializers.<b>err</b></code></a>

<a name="constructor"></a>
### pino([stream], [opts])
Expand Down Expand Up @@ -324,6 +325,24 @@ It returns an object in the form:
}
```
<a name="errSerializer"></a>
### pino.stdSerializers.err
Serializes an `Error` object if passed in as an property.
```js
{
"pid": 40510,
"hostname": "MBP-di-Matteo",
"level": 50,
"msg": "an error",
"time": 1459433282301,
"v": 1,
"type": "Error",
"stack": "Error: an error\n at Object.<anonymous> (/Users/matteo/Repositories/pino/example.js:16:7)\n at Module._compile (module.js:435:26)\n at Object.Module._extensions..js (module.js:442:10)\n at Module.load (module.js:356:32)\n at Function.Module._load (module.js:313:12)\n at Function.Module.runMain (module.js:467:10)\n at startup (node.js:136:18)\n at node.js:963:3"
}
```
<a name="rotate"></a>
## How do I rotate log files
Expand Down
11 changes: 10 additions & 1 deletion pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,18 @@ function asResValue (res) {
}
}

function asErrValue (err) {
return {
type: err.constructor.name,
message: err.message,
stack: err.stack
}
}

module.exports = pino

module.exports.stdSerializers = {
req: asReqValue,
res: asResValue
res: asResValue,
err: asErrValue
}
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ function levelTest (name, level) {
instance[name](err)
})

test('passing error with a serializer at level ' + name, function (t) {
t.plan(2)
var err = new Error('myerror')
var instance = pino({
serializers: {
err: pino.stdSerializers.err
}
}, sink(function (chunk, enc, cb) {
t.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
delete chunk.time
t.deepEqual(chunk, {
pid: pid,
hostname: hostname,
level: level,
err: {
type: 'Error',
message: err.message,
stack: err.stack
},
v: 1
})
cb()
}))

instance.level = name
instance[name]({ err: err })
})

test('child logger for level ' + name, function (t) {
t.plan(2)
var instance = pino(sink(function (chunk, enc, cb) {
Expand Down

0 comments on commit b26d617

Please sign in to comment.