Skip to content

Commit

Permalink
Merge bfc0ca4 into 36ffa6e
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jan 19, 2022
2 parents 36ffa6e + bfc0ca4 commit ed868b7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Readme.md
Expand Up @@ -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:

Expand All @@ -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+
Expand Down Expand Up @@ -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: {}
}
```
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -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
})
}

Expand Down
23 changes: 23 additions & 0 deletions test/basic.test.js
Expand Up @@ -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()
})

Expand Down

0 comments on commit ed868b7

Please sign in to comment.