Skip to content

Commit

Permalink
pass append option on pino/file transport to pino.destination function (
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Nov 18, 2021
1 parent 0ab984f commit bfce37c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
11 changes: 11 additions & 0 deletions docs/transports.md
Expand Up @@ -251,6 +251,17 @@ const transport = pino.transport({
pino(transport)
```
By default, the `pino/file` transport appends to the destination file if it exists. The `append` option may be set to `false` to configure the transport to truncate the file upon opening it for writing.
```js
const pino = require('pino')
const transport = pino.transport({
target: 'pino/file',
options: { destination: '/path/to/file', append: false }
})
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 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
1 change: 1 addition & 0 deletions file.js
Expand Up @@ -5,6 +5,7 @@ const { once } = require('events')

module.exports = async function (opts = {}) {
const destOpts = { dest: opts.destination || 1, sync: false }
if (opts.append === false) destOpts.append = false
if (opts.mkdir) destOpts.mkdir = true
const destination = pino.destination(destOpts)
await once(destination, 'ready')
Expand Down
24 changes: 22 additions & 2 deletions test/helper.js
Expand Up @@ -3,7 +3,7 @@
const os = require('os')
const writer = require('flush-write-stream')
const split = require('split2')
const { existsSync, statSync } = require('fs')
const { existsSync, readFileSync, statSync } = require('fs')
const pid = process.pid
const hostname = os.hostname()

Expand Down Expand Up @@ -77,4 +77,24 @@ function watchFileCreated (filename) {
})
}

module.exports = { getPathToNull, sink, check, once, sleep, watchFileCreated, isWin, isYarnPnp }
function watchForWrite (filename, testString) {
return new Promise((resolve, reject) => {
const TIMEOUT = 2000
const INTERVAL = 100
const threshold = TIMEOUT / INTERVAL
let counter = 0
const interval = setInterval(() => {
if (readFileSync(filename).includes(testString)) {
clearInterval(interval)
resolve()
} else if (counter <= threshold) {
counter++
} else {
clearInterval(interval)
reject(new Error(`'${testString}' hasn't been written to ${filename} within ${TIMEOUT} ms.`))
}
}, INTERVAL)
})
}

module.exports = { getPathToNull, sink, check, once, sleep, watchFileCreated, watchForWrite, isWin, isYarnPnp }
28 changes: 26 additions & 2 deletions test/transport/core.test.js
Expand Up @@ -3,8 +3,8 @@
const os = require('os')
const { join } = require('path')
const { once } = require('events')
const { readFile } = require('fs').promises
const { watchFileCreated } = require('../helper')
const { readFile, writeFile } = require('fs').promises
const { watchFileCreated, watchForWrite } = require('../helper')
const { test } = require('tap')
const pino = require('../../')
const url = require('url')
Expand Down Expand Up @@ -300,6 +300,30 @@ test('pino.transport with target pino/file and mkdir option', async ({ same, tea
})
})

test('pino.transport with target pino/file and append option', async ({ same, teardown }) => {
const destination = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
await writeFile(destination, JSON.stringify({ pid, hostname, time: Date.now(), level: 30, msg: 'hello' }))
const transport = pino.transport({
target: 'pino/file',
options: { destination, append: false }
})
teardown(transport.end.bind(transport))
const instance = pino(transport)
instance.info('goodbye')
await watchForWrite(destination, '"goodbye"')
const result = JSON.parse(await readFile(destination))
delete result.time
same(result, {
pid,
hostname,
level: 30,
msg: 'goodbye'
})
})

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

0 comments on commit bfce37c

Please sign in to comment.