Skip to content

Commit

Permalink
Coerce string integer destinations to file descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Oct 24, 2021
1 parent d7f6490 commit d09c75f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/transports.md
Expand Up @@ -220,7 +220,7 @@ const transport = pino.transport({
pino(transport)
```
The `options.destination` property may also be a number to represent a file descriptor. Typically this would be `1` to write to STDOUT or `2` to write to STDERR. If `options.destination` is not set, it defaults to `1` which means logs will be written to STDOUT.
The `options.destination` property may also be a number to represent a filedescriptor. Typically this would be `1` to write to STDOUT or `2` to write to STDERR. If `options.destination` is not set, it defaults to `1` which means logs will be written to STDOUT. If `options.destination` is a string integer, e.g. `'1'`, it will be coerced to a number and used as a file descriptor. If this is not desired, provide a full path, e.g. `/tmp/1`.
The difference between using the `pino/file` transport builtin and using `pino.destination` is that `pino.destination` runs in the main thread, whereas `pino/file` sets up `pino.destination` in a worker thread.
Expand Down
10 changes: 9 additions & 1 deletion file.js
Expand Up @@ -4,7 +4,15 @@ const pino = require('./pino')
const { once } = require('events')

module.exports = async function (opts = {}) {
const destination = pino.destination({ dest: opts.destination || 1, sync: false })
const destination = pino.destination({ dest: normalizeDest(opts.destination || 1), sync: false })
await once(destination, 'ready')
return destination
}

function normalizeDest (destination) {
const fd = Number(destination)
if (typeof destination === 'string' && Number.isFinite(fd)) {
return fd
}
return destination
}
9 changes: 9 additions & 0 deletions test/fixtures/transport-string-stdout.js
@@ -0,0 +1,9 @@
'use strict'

const pino = require('../..')
const transport = pino.transport({
target: 'pino/file',
options: { destination: '1' }
})
const logger = pino(transport)
logger.info('Hello')
12 changes: 12 additions & 0 deletions test/transport/core.test.js
Expand Up @@ -305,6 +305,18 @@ test('stdout in worker', async ({ not }) => {
not(strip(actual).match(/Hello/), null)
})

test('string integer destination', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-string-stdout.js')])

child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/Hello/), null)
})

test('pino transport options with target', async ({ teardown, same }) => {
const destination = join(
os.tmpdir(),
Expand Down

0 comments on commit d09c75f

Please sign in to comment.