Skip to content

Commit

Permalink
Merge pull request #4 from pinojs/wrapper
Browse files Browse the repository at this point in the history
Alternative implementation of the meta wrapper
  • Loading branch information
jsumners committed Apr 4, 2018
2 parents 2d90f25 + 9fa9627 commit 875a019
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 30 deletions.
36 changes: 21 additions & 15 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,34 @@ in [CLI Arguments](#cliargs):
levelFirst: false, // --levelFirst
localTime: false, // --localTime
messageKey: 'msg', // --messageKey
translateTime: false, // --translateTime
useMetadata: false,
outputStream: process.stdout
translateTime: false // --translateTime
}
```

Unless `useMetadata` is set to `true`, the factory function returns the
function: `function pretty (line) {}`. See the [next subsection](#usemetadata)
for information on the other case.
See the [next subsection](#usemetadata) for information on how to use this
directly with `pino`.

<a id="usemetadata"></a>
### `useMetadata` and `outputStream`
### pretty.asMetaWrapper(writable)

If the `useMetadata` option is set to `true`, then the factory function will
return an object that can be supplied directly to Pino as a stream that is
compatible with Pino's [metadata stream API][mdstream]. This allows `pino-pretty`
to skip the expensive task of parsing JSON log lines and instead work directly
with Pino's log object.
```js
const factory = require('pino-pretty')
const pino = require('pino')

// writable is any Writable stream
const writable = process.stdout
const dest = factory({ colorize: true }).asMetaWrapper(writable)

const logger = pino({}, dest)
```

The function returned by the factory has a `.asMetaWrapper(dest)` function attached
which will return an object that can be supplied directly to Pino as a stream
that is compatible with Pino's [metadata stream API][mdstream].
This allows `pino-pretty` to skip the expensive task of parsing JSON log lines
and instead work directly with Pino's log object.

When `useMetadata` is set to `true` then the `outputStream` option dictates
where the final formatted log line will be written. This must be a writable
stream. The default stream is `process.stdout`.
The default stream is `process.stdout`.

[mdstream]: https://github.com/pinojs/pino/blob/fc4c83b/docs/API.md#metadata

Expand Down
52 changes: 39 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,9 @@ module.exports = function prettyFactory (options) {
color.message = ctx.cyan
}

if (!opts.useMetadata) {
return pretty
}
pretty.asMetaWrapper = asMetaWrapper

const outputSym = Symbol('pino-pretty.out')

return {
[Symbol.for('needsMetadata')]: true,
[outputSym]: opts.outputStream,
write (chunk) {
const formatted = pretty(chunk)
this[outputSym].write(formatted)
}
}
return pretty

function pretty (inputData) {
let log
Expand Down Expand Up @@ -249,3 +238,40 @@ module.exports = function prettyFactory (options) {
}
}
}

function asMetaWrapper (dest) {
const parsed = Symbol('parsedChindings')

if (!dest) {
dest = process.stdout
} else if (!dest.write) {
throw new Error('the destination must be writable')
}

const pretty = this

return {
[Symbol.for('needsMetadata')]: true,
lastLevel: 0,
lastMsg: null,
lastObj: null,
lastLogger: null,
write (chunk) {
var chindings = this.lastLogger[parsed]

if (!chindings) {
chindings = JSON.parse('{"v":1' + this.lastLogger.chindings + '}')
this.lastLogger[parsed] = chindings
}

const obj = Object.assign({
level: this.lastLevel,
msg: this.lastMsg,
time: this.lastTime
}, chindings, this.lastObj)

const formatted = pretty(obj)
dest.write(formatted)
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"through2": "^2.0.3"
},
"devDependencies": {
"pino": "^4.15.3",
"pino": "^4.16.0",
"pre-commit": "^1.2.2",
"snazzy": "^7.1.1",
"standard": "^11.0.1",
Expand Down
21 changes: 20 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,26 @@ test('basic prettifier tests', (t) => {
cb()
}
})
const prettyStream = prettyFactory({useMetadata: true, outputStream: dest})
const prettyStream = prettyFactory().asMetaWrapper(dest)
const log = pino({}, prettyStream)
log.info('foo')
})

t.test('can swap date and level position through meta stream', (t) => {
t.plan(1)

const dest = new Writable({
objectMode: true,
write (formatted, enc, cb) {
t.is(
formatted,
`INFO [${epoch}] (${pid} on ${hostname}): foo\n`
)
cb()
}
})

const prettyStream = prettyFactory({ levelFirst: true }).asMetaWrapper(dest)
const log = pino({}, prettyStream)
log.info('foo')
})
Expand Down

0 comments on commit 875a019

Please sign in to comment.