Skip to content

Commit

Permalink
Bind pino instance to prettifier (#721)
Browse files Browse the repository at this point in the history
* feat(prettifier): bind pino instance to prettier

* style(lint): fix codestyle

* test: add tests

* docs(pretty): add info

* fix(docs): docs fix

* fix: bind prettifier

* fix: pass instance directly
  • Loading branch information
SkeLLLa committed Feb 28, 2020
1 parent 72eb875 commit 092eb8b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/pretty.md
Expand Up @@ -23,8 +23,12 @@ The API requires modules provide a factory function which returns a prettifier
function. This prettifier function must accept either a string of NDJSON or
a Pino log object. A psuedo-example of such a prettifier is:

The uninitialized Pino instance is passed as `this` into prettifier factory function,
so it can be accessed via closure by the returned prettifier function.

```js
module.exports = function myPrettifier (options) {
// `this` is bound to the pino instance
// Deal with whatever options are supplied.
return function prettifier (inputData) {
let logObject
Expand Down
7 changes: 4 additions & 3 deletions lib/tools.js
Expand Up @@ -152,8 +152,9 @@ function asChindings (instance, bindings) {
return data
}

function getPrettyStream (opts, prettifier, dest) {
function getPrettyStream (opts, prettifier, dest, instance) {
if (prettifier && typeof prettifier === 'function') {
prettifier = prettifier.bind(instance)
return prettifierMetaWrapper(prettifier(opts), dest)
}
try {
Expand Down Expand Up @@ -272,7 +273,7 @@ function buildSafeSonicBoom (dest, buffer = 0, sync = true) {
}

function createArgsNormalizer (defaultOptions) {
return function normalizeArgs (opts = {}, stream) {
return function normalizeArgs (instance, opts = {}, stream) {
// support stream as a string
if (typeof opts === 'string') {
stream = buildSafeSonicBoom(opts)
Expand Down Expand Up @@ -306,7 +307,7 @@ function createArgsNormalizer (defaultOptions) {
}
if (prettyPrint) {
const prettyOpts = Object.assign({ messageKey }, prettyPrint)
stream = getPrettyStream(prettyOpts, prettifier, stream)
stream = getPrettyStream(prettyOpts, prettifier, stream, instance)
}
return { opts, stream }
}
Expand Down
7 changes: 4 additions & 3 deletions pino.js
Expand Up @@ -61,7 +61,8 @@ const normalize = createArgsNormalizer(defaultOptions)
const serializers = Object.assign(Object.create(null), stdSerializers)

function pino (...args) {
const { opts, stream } = normalize(...args)
const instance = {}
const { opts, stream } = normalize(instance, ...args)
const {
redact,
crlf,
Expand Down Expand Up @@ -102,7 +103,7 @@ function pino (...args) {
assertDefaultLevelFound(level, customLevels, useOnlyCustomLevels)
const levels = mappings(customLevels, useOnlyCustomLevels)

const instance = {
Object.assign(instance, {
levels,
[useLevelLabelsSym]: useLevelLabels,
[levelKeySym]: levelKey,
Expand All @@ -119,7 +120,7 @@ function pino (...args) {
[serializersSym]: serializers,
[mixinSym]: mixin,
[chindingsSym]: chindings
}
})
Object.setPrototypeOf(instance, proto)

if (customLevels || useLevelLabels || levelKey !== defaultOptions.levelKey) genLsCache(instance)
Expand Down
41 changes: 41 additions & 0 deletions test/custom-levels.test.js
Expand Up @@ -265,3 +265,44 @@ test('custom level does not affect levelKey', async ({ is }) => {
const { priority } = await once(stream, 'data')
is(priority, 35)
})

test('custom levels accesible in prettifier function', async ({ plan, same }) => {
plan(1)
const logger = pino({
prettyPrint: true,
prettifier: function prettifierFactory () {
const instance = this
return function () {
same(instance.levels, {
labels: {
10: 'trace',
20: 'debug',
30: 'info',
35: 'foo',
40: 'warn',
45: 'bar',
50: 'error',
60: 'fatal'
},
values: {
trace: 10,
debug: 20,
info: 30,
warn: 40,
error: 50,
fatal: 60,
foo: 35,
bar: 45
}
})
}
},
customLevels: {
foo: 35,
bar: 45
},
changeLevelName: 'priority'
})

logger.foo('test')
})

0 comments on commit 092eb8b

Please sign in to comment.