Skip to content

Commit c3a1348

Browse files
authoredNov 3, 2021
Support SonicBoom as a destination (#256)
1 parent 6037ddc commit c3a1348

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed
 

‎Readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ The options accepted have keys corresponding to the options described in [CLI Ar
207207
ignore: 'pid,hostname', // --ignore
208208
hideObject: false, // --hideObject
209209
singleLine: false, // --singleLine
210+
211+
// The file or file descriptor (1 is stdout) to write to
212+
destination: 1,
213+
214+
// Alternatively, pass a `sonic-boom` instance (allowing more flexibility):
215+
// destination: new SonicBoom({ dest: 'a/file', mkdir: true })
216+
210217
customPrettifiers: {}
211218
}
212219
```

‎index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,18 @@ function build (opts = {}) {
183183
}
184184
})
185185

186-
const destination = sonic({ dest: opts.destination || 1, sync: false })
186+
let destination
187+
188+
if (typeof opts.destination === 'object' && typeof opts.destination.write === 'function') {
189+
destination = opts.destination
190+
} else {
191+
destination = sonic({
192+
dest: opts.destination || 1,
193+
append: opts.append,
194+
mkdir: opts.mkdir,
195+
sync: false
196+
})
197+
}
187198
/* istanbul ignore else */
188199
if (destination.fd === 1) {
189200
// We cannot close the output

‎test/basic.test.js

+59
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ const os = require('os')
55
const test = require('tap').test
66
const pino = require('pino')
77
const dateformat = require('dateformat')
8+
const path = require('path')
9+
const rimraf = require('rimraf')
10+
const { join } = require('path')
11+
const fs = require('fs')
812
const pinoPretty = require('..')
13+
const SonicBoom = require('sonic-boom')
914
const _prettyFactory = pinoPretty.prettyFactory
1015

16+
// Disable pino warnings
17+
process.removeAllListeners('warning')
18+
1119
function prettyFactory (opts) {
1220
if (!opts) {
1321
opts = { colorize: false }
@@ -748,5 +756,56 @@ test('basic prettifier tests', (t) => {
748756
t.doesNotThrow(pinoPretty)
749757
})
750758

759+
t.test('stream usage', async (t) => {
760+
t.plan(1)
761+
const tmpDir = path.join(__dirname, '.tmp_' + Date.now())
762+
t.teardown(() => rimraf(tmpDir, noop))
763+
764+
const destination = join(tmpDir, 'output')
765+
766+
const pretty = pinoPretty({
767+
singleLine: true,
768+
colorize: false,
769+
mkdir: true,
770+
append: false,
771+
destination: new SonicBoom({ dest: destination, async: false, mkdir: true, append: true }),
772+
customPrettifiers: {
773+
upper: val => val.toUpperCase(),
774+
undef: () => undefined
775+
}
776+
})
777+
const log = pino(pretty)
778+
log.info({ msg: 'message', extra: { foo: 'bar', number: 42 }, upper: 'foobar', undef: 'this will not show up' })
779+
780+
await watchFileCreated(destination)
781+
782+
const formatted = fs.readFileSync(destination, 'utf8')
783+
784+
t.equal(formatted, `[${epoch}] INFO (${pid} on ${hostname}): message {"extra":{"foo":"bar","number":42},"upper":"FOOBAR"}\n`)
785+
})
786+
751787
t.end()
752788
})
789+
790+
function watchFileCreated (filename) {
791+
return new Promise((resolve, reject) => {
792+
const TIMEOUT = 2000
793+
const INTERVAL = 100
794+
const threshold = TIMEOUT / INTERVAL
795+
let counter = 0
796+
const interval = setInterval(() => {
797+
// On some CI runs file is created but not filled
798+
if (fs.existsSync(filename) && fs.statSync(filename).size !== 0) {
799+
clearInterval(interval)
800+
resolve()
801+
} else if (counter <= threshold) {
802+
counter++
803+
} else {
804+
clearInterval(interval)
805+
reject(new Error(`${filename} was not created.`))
806+
}
807+
}, INTERVAL)
808+
})
809+
}
810+
811+
function noop () {}

0 commit comments

Comments
 (0)
Please sign in to comment.