Skip to content

Commit

Permalink
Merge 3679216 into 0cc550a
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Sep 15, 2021
2 parents 0cc550a + 3679216 commit 8f5db0f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,43 @@ stream, it emits the following events:

#### Options

* `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
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ const split = require('split2')

module.exports = function build (fn, opts = {}) {
const parseLines = opts.parse === 'lines'
const parseLine = typeof opts.parseLine === 'function' ? opts.parseLine : JSON.parse
const close = opts.close || defaultClose
const stream = split(function (line) {
let value

try {
value = JSON.parse(line)
value = parseLine(line)
} catch (error) {
this.emit('unknown', line, error)
return
Expand Down
42 changes: 42 additions & 0 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,48 @@ test('parse lines', ({ same, plan, equal }) => {
stream.end()
})

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 }) => {
plan(9)

Expand Down

0 comments on commit 8f5db0f

Please sign in to comment.