Skip to content

Commit

Permalink
Use required prettifier directly and tweak documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Apr 5, 2018
1 parent d7d52a1 commit e99e957
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
29 changes: 20 additions & 9 deletions docs/pretty.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
By default, Pino log lines are newline delimited JSON. This is perfect for
production usage and long term storage. It's not so great for development
environments. Thus, Pino logs can be prettified by using a Pino prettifier
module.
module like [`pino-pretty`][pp]:

Pino prettifier modules are extra modules that provide [metadata streams][mdstreams].
These modules provide a factory function which return a prettifier function.
This prettifier function has an `asMetaWrapper(dest)` method attached to it.
A psuedo-example is:
```sh
$ cat app.log | pino-pretty
```

## Prettifier API

Pino prettifier modules are extra modules that provide a CLI for parsing ndJSON
log lines piped via `stdin` and expose an API which conforms to the Pino
[metadata streams](API.md#metadata) API.

The API requires modules provide a factory function which return a prettifier
function. This prettifier function has an `asMetaWrapper(dest)` method attached
to it. A psuedo-example is:

```js
module.exports = function myPrettifier (options) {
Expand Down Expand Up @@ -47,17 +56,19 @@ To use pretty printing in your project:
const pino = require('pino')
const log = pino({
prettyPrint: {
prettifier: 'pino-pretty'
}
levelFirst: true
},
prettifier: require('pino-pretty')
})
```
Note: the default prettifier module is `pino-pretty`, so the preceeding
example could be:
```js
const isdebug = require('isdebug') // true when NODE_ENV=development
const pino = require('pino')
const log = pino({
prettyPrint: isdebug
prettyPrint: {
levelFirst: true
}
})
```
See the [`pino-pretty` documentation][pp] for more information on the options
Expand Down
13 changes: 7 additions & 6 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,15 @@ Object.defineProperty(pinoPrototype, 'isLevelEnabled', {
value: isLevelEnabled
})

function getPrettyStream (opts) {
var prettyFactory
var prettyModule = opts.prettifier || 'pino-pretty'
function getPrettyStream (opts, prettifier) {
if (prettifier && typeof prettifier === 'function') {
return prettifier(opts).asMetaWrapper(process.stdout)
}
try {
prettyFactory = require(prettyModule)
var prettyFactory = require('pino-pretty')
return prettyFactory(opts).asMetaWrapper(process.stdout)
} catch (e) {
throw Error(`Missing ${prettyModule} module: ${prettyModule} must be installed separately`)
throw Error('Missing `pino-pretty` module: `pino-pretty` must be installed separately')
}
}

Expand All @@ -297,7 +298,7 @@ function pino (opts, stream) {
if (!isStdout && iopts.prettyPrint) throw Error('cannot enable pretty print when stream is not process.stdout')
if (iopts.prettyPrint) {
var prettyOpts = Object.assign({ messageKey: iopts.messageKey }, iopts.prettyPrint)
var pstream = getPrettyStream(prettyOpts)
var pstream = getPrettyStream(prettyOpts, iopts.prettifier)
istream = pstream
}

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/pretty/prettyFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global.process = { __proto__: process, pid: 123456 }
Date.now = function () { return 1459875739796 }
require('os').hostname = function () { return 'abcdefghijklmnopqr' }
var pino = require(require.resolve('./../../../'))
var log = pino({prettyPrint: {levelFirst: true}, prettifier: require('pino-pretty')})
log.info('h')
15 changes: 15 additions & 0 deletions test/pretty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ test('can be enabled via constructor with pretty configuration', function (t) {
})
})

test('can be enabled via constructor with prettifier', function (t) {
t.plan(1)
var actual = ''
var child = fork(path.join(__dirname, 'fixtures', 'pretty', 'prettyFactory.js'), {silent: true})

child.stdout.pipe(writeStream(function (s, enc, cb) {
actual += s
cb()
}))

child.on('close', function () {
t.notEqual(actual.match(/^INFO.*h/), null)
})
})

test('throws error when enabled with stream specified', function (t) {
t.plan(1)
var logStream = writeStream(function (s, enc, cb) {
Expand Down

0 comments on commit e99e957

Please sign in to comment.