From d63257dce64861bdebe5b3a4f342c226e40c8c35 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Sun, 24 Oct 2021 18:17:03 -0400 Subject: [PATCH] Coerce string integer destinations to file descriptors --- docs/transports.md | 2 +- test/fixtures/transport-string-stdout.js | 9 +++++++++ test/transport/core.test.js | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/transport-string-stdout.js diff --git a/docs/transports.md b/docs/transports.md index bafb853f3..9a1295db5 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -262,7 +262,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. diff --git a/test/fixtures/transport-string-stdout.js b/test/fixtures/transport-string-stdout.js new file mode 100644 index 000000000..64d8ac1ea --- /dev/null +++ b/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') diff --git a/test/transport/core.test.js b/test/transport/core.test.js index 01da9db64..af1e775e2 100644 --- a/test/transport/core.test.js +++ b/test/transport/core.test.js @@ -377,6 +377,18 @@ test('log and exit before ready', 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(),