diff --git a/Readme.md b/Readme.md index 1df59634..da54191f 100644 --- a/Readme.md +++ b/Readme.md @@ -153,6 +153,9 @@ logger.info('hi') See the [Options](#options) section for all possible options. + +### Usage as a stream + If you are using `pino-pretty` as a stream and you need to provide options to `pino`, pass the options as the first argument and `pino-pretty` as second argument: @@ -168,7 +171,24 @@ const logger = pino({ level: 'info' }, stream) logger.debug('hi') ``` +### Usage with Jest + +Logging with Jest is _problematic_, as the test framework requires no asynchronous operation to +continue after the test has finished. The following is the only supported way to use this module +with Jest: + +```js +import pino from 'pino' +import pretty from 'pino-pretty' + +test('test pino-pretty', () => { + const logger = pino(pretty({ sync: true })); + logger.info('Info'); + logger.error('Error'); +}); +``` +>>>>>>> Add support for sync: true as option ### Handling non-serializable options Using the new [pino v7+ @@ -230,6 +250,12 @@ The options accepted have keys corresponding to the options described in [CLI Ar // Alternatively, pass a `sonic-boom` instance (allowing more flexibility): // destination: new SonicBoom({ dest: 'a/file', mkdir: true }) + // You can also configure some SonicBoom options directly + sync: false, // by default we write asynchronously + append: true, // the file is opened with the 'a' flag + mdkdir: true, // create the target destination + + customPrettifiers: {} } ``` diff --git a/index.js b/index.js index bc12a6e9..6b56d49c 100644 --- a/index.js +++ b/index.js @@ -193,7 +193,7 @@ function build (opts = {}) { dest: opts.destination || 1, append: opts.append, mkdir: opts.mkdir, - sync: false + sync: opts.sync // by default sonic will be async }) } diff --git a/test/basic.test.js b/test/basic.test.js index 01c91464..a2af4684 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -961,6 +961,29 @@ test('basic prettifier tests', (t) => { t.equal(formatted, `[${epoch}] INFO (${pid} on ${hostname}): message {"extra":{"foo":"bar","number":42},"upper":"FOOBAR"}\n`) }) + t.test('sync option', async (t) => { + t.plan(1) + const tmpDir = path.join(__dirname, '.tmp_' + Date.now()) + t.teardown(() => rimraf(tmpDir, noop)) + + const destination = join(tmpDir, 'output') + + const pretty = pinoPretty({ + singleLine: true, + colorize: false, + mkdir: true, + append: false, + sync: true, + destination + }) + const log = pino(pretty) + log.info({ msg: 'message', extra: { foo: 'bar', number: 42 }, upper: 'foobar' }) + + const formatted = fs.readFileSync(destination, 'utf8') + + t.equal(formatted, `[${epoch}] INFO (${pid} on ${hostname}): message {"extra":{"foo":"bar","number":42},"upper":"foobar"}\n`) + }) + t.end() })