pino-abstract-transport
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -8,2 +8,3 @@ 'use strict' | ||
const parseLines = opts.parse === 'lines' | ||
const parseLine = typeof opts.parseLine === 'function' ? opts.parseLine : JSON.parse | ||
const close = opts.close || defaultClose | ||
@@ -14,3 +15,3 @@ const stream = split(function (line) { | ||
try { | ||
value = JSON.parse(line) | ||
value = parseLine(line) | ||
} catch (error) { | ||
@@ -17,0 +18,0 @@ this.emit('unknown', line, error) |
{ | ||
"name": "pino-abstract-transport", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Write Pino transports easily", | ||
@@ -29,3 +29,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"husky": "^6.0.0", | ||
"husky": "^7.0.0", | ||
"snazzy": "^9.0.0", | ||
@@ -32,0 +32,0 @@ "standard": "^16.0.3", |
@@ -19,3 +19,3 @@ # pino-abstract-transport | ||
```js | ||
import build from 'pino-abstract-stream' | ||
import build from 'pino-abstract-transport' | ||
@@ -36,3 +36,3 @@ exports default async function (opts) { | ||
const build = require('pino-abstract-stream') | ||
const build = require('pino-abstract-transport') | ||
@@ -65,7 +65,41 @@ module.exports = function (opts) { | ||
* `parse` an option to change to data format passed to build function. Default: `undefined`. | ||
* `close(err, cb)` a function that is called to shutdown the transport. It's called both on error and non-error shutdowns. | ||
It can also return a promise. In this case discard the the `cb` argument. | ||
* `parseLine(line)` a function that is used to parse line recieved from `pino`. | ||
## Example | ||
### custom parseLine | ||
You can allow custom `parseLine` from users while providing a simple and safe default parseLine. | ||
```js | ||
'use strict' | ||
const build = require('pino-abstract-transport') | ||
function defaultParseLine (line) { | ||
const obj = JSON.parse(line) | ||
// property foo will be added on each line | ||
obj.foo = 'bar' | ||
return obj | ||
} | ||
module.exports = function (opts) { | ||
const parseLine = typeof opts.parseLine === 'function' ? opts.parseLine : defaultParseLine | ||
return build(function (source) { | ||
source.on('data', function (obj) { | ||
console.log(obj) | ||
}) | ||
}, { | ||
parseLine: parseLine | ||
}) | ||
} | ||
``` | ||
## License | ||
MIT |
@@ -209,2 +209,44 @@ 'use strict' | ||
test('custom parse line function', ({ same, plan, equal }) => { | ||
plan(11) | ||
const expected = [{ | ||
level: 30, | ||
time: 1617955768092, | ||
pid: 2942, | ||
hostname: 'MacBook-Pro.local', | ||
msg: 'hello world' | ||
}, { | ||
level: 30, | ||
time: 1617955768092, | ||
pid: 2942, | ||
hostname: 'MacBook-Pro.local', | ||
msg: 'another message', | ||
prop: 42 | ||
}] | ||
let num = 0 | ||
function parseLine (str) { | ||
const obj = JSON.parse(str) | ||
same(expected[num], obj) | ||
return obj | ||
} | ||
const stream = build(function (source) { | ||
source.on('data', function (line) { | ||
const obj = expected[num] | ||
same(this.lastLevel, obj.level) | ||
same(this.lastTime, obj.time) | ||
same(this.lastObj, obj) | ||
same(obj, line) | ||
num++ | ||
}) | ||
}, { metadata: true, parseLine }) | ||
equal(stream[Symbol.for('pino.metadata')], true) | ||
const lines = expected.map(JSON.stringify).join('\n') | ||
stream.write(lines) | ||
stream.end() | ||
}) | ||
test('set metadata (default)', ({ same, plan, equal }) => { | ||
@@ -211,0 +253,0 @@ plan(9) |
Sorry, the diff of this file is not supported yet
16360
396
103