Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass append option on pino/file transport to pino.destination function #1229

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Comment on lines 7 to 10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not be more comprehensive?

const destOpts = Object.assign({}, opts.destination, { dest: opts.destination || 1, sync: false })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's certainly a valid approach, but it would expand the scope of this issue. If you prefer to forward all the options to the destination, I won't object. @mcollina, where do you stand on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go for it, it's better

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