Skip to content

Commit

Permalink
add transports constructor option (#1111)
Browse files Browse the repository at this point in the history
* add transports constructor option

* rename option

* docs: add transport option

* fix tests

* Apply suggestions from code review

Co-authored-by: Matteo Collina <matteo.collina@gmail.com>

* docs: add more examples

* Apply suggestions from code review

Co-authored-by: James Sumners <james@sumners.email>

* Apply suggestions from code review

Co-authored-by: David Mark Clements <david.mark.clements@gmail.com>

* Apply suggestions from code review

Co-authored-by: James Sumners <james@sumners.email>

* fix test

Co-authored-by: Matteo Collina <matteo.collina@gmail.com>
Co-authored-by: James Sumners <james@sumners.email>
Co-authored-by: David Mark Clements <david.mark.clements@gmail.com>
  • Loading branch information
4 people committed Sep 8, 2021
1 parent f78dd5c commit bd67ef9
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
31 changes: 31 additions & 0 deletions docs/api.md
Expand Up @@ -369,6 +369,37 @@ documented in the [Browser API ⇗](/docs/browser.md) documentation.

* See [Browser API ⇗](/docs/browser.md)

#### `transport` (Object)

The `transport` option is a shorthand for the [pino.transport()](#pino-transport) function.
It supports the same input options:
```js
require('pino')({
transport: {
target: '/absolute/path/to/my-transport.mjs'
}
})

// or multiple transports
require('pino')({
transport: {
targets: [
{ target: '/absolute/path/to/my-transport.mjs', level: 'error' },
{ target: 'some-file-transport', options: { destination: '/dev/null' }
]
}
})
```
If the transport option is supplied to `pino`, a [`destination`](#destination) parameter may not also be passed as a separate argument to `pino`:
```js
pino({ transport: {}}, '/path/to/somewhere') // THIS WILL NOT WORK, DO NOT DO THIS
pino({ transport: {}}, process.stderr) // THIS WILL NOT WORK, DO NOT DO THIS
when using the `transport` option. In this case an `Error` will be thrown.

* See [pino.transport()](#pino-transport)

<a id="destination"></a>
### `destination` (SonicBoom | WritableStream | String | Object)

Expand Down
7 changes: 7 additions & 0 deletions lib/tools.js
Expand Up @@ -27,6 +27,7 @@ const {
nestedKeyStrSym
} = require('./symbols')
const { isMainThread } = require('worker_threads')
const transport = require('./transport')

function noop () {}

Expand Down Expand Up @@ -395,10 +396,16 @@ function createArgsNormalizer (defaultOptions) {
stream = buildSafeSonicBoom({ dest: opts, sync: true })
opts = {}
} else if (typeof stream === 'string') {
if (opts && opts.transport) {
throw Error('only one of option.transport or stream can be specified')
}
stream = buildSafeSonicBoom({ dest: stream, sync: true })
} else if (opts instanceof SonicBoom || opts.writable || opts._writableState) {
stream = opts
opts = null
} else if (opts.transport) {
stream = transport(opts.transport)
opts = null
}
opts = Object.assign({}, defaultOptions, opts)
opts.serializers = Object.assign({}, defaultOptions.serializers, opts.serializers)
Expand Down
2 changes: 1 addition & 1 deletion lib/transport.js
Expand Up @@ -64,7 +64,7 @@ function transport (fullOptions) {
let target = fullOptions.target

if (target && targets) {
throw new Error('Only one of target or targets can be specified')
throw new Error('only one of target or targets can be specified')
}

if (targets) {
Expand Down
94 changes: 93 additions & 1 deletion test/transport.test.js
Expand Up @@ -299,7 +299,7 @@ test('pino.transport with target and targets', async ({ fail, equal }) => {
})
fail('must throw')
} catch (err) {
equal(err.message, 'Only one of target or targets can be specified')
equal(err.message, 'only one of target or targets can be specified')
}
})

Expand Down Expand Up @@ -386,3 +386,95 @@ test('stdout in worker', async ({ not }) => {
await once(child, 'close')
not(strip(actual).match(/Hello/), null)
})

test('pino transport options with target', async ({ teardown, same }) => {
const destination = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const instance = pino({
transport: {
target: '#pino/file',
options: { destination }
}
})
const transportStream = instance[pino.symbols.streamSym]
teardown(transportStream.end.bind(transportStream))
instance.info('transport option test')
await watchFileCreated(destination)
const result = JSON.parse(await readFile(destination))
delete result.time
same(result, {
pid,
hostname,
level: 30,
msg: 'transport option test'
})
})

test('pino transport options with targets', async ({ teardown, same }) => {
const dest1 = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const dest2 = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const instance = pino({
transport: {
targets: [
{ target: '#pino/file', options: { destination: dest1 } },
{ target: '#pino/file', options: { destination: dest2 } }
]
}
})
const transportStream = instance[pino.symbols.streamSym]
teardown(transportStream.end.bind(transportStream))
instance.info('transport option test')

await Promise.all([watchFileCreated(dest1), watchFileCreated(dest2)])
const result1 = JSON.parse(await readFile(dest1))
delete result1.time
same(result1, {
pid,
hostname,
level: 30,
msg: 'transport option test'
})
const result2 = JSON.parse(await readFile(dest2))
delete result2.time
same(result2, {
pid,
hostname,
level: 30,
msg: 'transport option test'
})
})

test('transport options with target and targets', async ({ fail, equal }) => {
try {
pino({
transport: {
target: {},
targets: {}
}
})
fail('must throw')
} catch (err) {
equal(err.message, 'only one of target or targets can be specified')
}
})

test('transport options with target and stream', async ({ fail, equal }) => {
try {
pino({
transport: {
target: {}
}
}, '/log/null')
fail('must throw')
} catch (err) {
equal(err.message, 'only one of option.transport or stream can be specified')
}
})

0 comments on commit bd67ef9

Please sign in to comment.