Comparing version 7.8.1 to 7.9.0
@@ -122,4 +122,4 @@ # API | ||
If provided, the `mixin` function is called each time one of the active | ||
logging methods is called. The first and only parameter is the value `mergeObject` or an empty object. The function must synchronously return an | ||
object. The properties of the returned object will be added to the | ||
logging methods is called. The first parameter is the value `mergeObject` or an empty object. The second parameter is the leg level number. | ||
The function must synchronously return an object. The properties of the returned object will be added to the | ||
logged JSON. | ||
@@ -157,3 +157,3 @@ | ||
}, 'Message 1') | ||
// {"level":30,"time":1591195061437,"pid":16012,"hostname":"x","appName":"My app","description":"Ok" "msg":"Message 1"} | ||
// {"level":30,"time":1591195061437,"pid":16012,"hostname":"x","appName":"My app","description":"Ok","msg":"Message 1"} | ||
logger.info('Message 2') | ||
@@ -164,2 +164,18 @@ // {"level":30,"time":1591195061437,"pid":16012,"hostname":"x","appName":"My app","description":"Ok","msg":"Message 2"} | ||
The `mixin` method can be used to add the level label to each log message such as in the following example: | ||
```js | ||
const logger = pino({ | ||
mixin(_context, level) { | ||
return { 'level-label': logger.levels.labels[level] } | ||
} | ||
}) | ||
logger.info({ | ||
description: 'Ok' | ||
}, 'Message 1') | ||
// {"level":30,"time":1591195061437,"pid":16012,"hostname":"x","appName":"My app","description":"Ok","level-label":"info","msg":"Message 1"} | ||
logger.error('Message 2') | ||
// {"level":30,"time":1591195061437,"pid":16012,"hostname":"x","appName":"My app","description":"Ok","level-label":"error","msg":"Message 2"} | ||
``` | ||
If the `mixin` feature is being used merely to add static metadata to each log message, | ||
@@ -166,0 +182,0 @@ then a [child logger ⇗](/docs/child-loggers.md) should be used instead. |
@@ -120,3 +120,8 @@ 'use strict' | ||
formatters.level, | ||
resetChildingsFormatter, | ||
formatters.bindings | ||
? (bindings) => ({ | ||
...formatters.bindings(JSON.parse('{' + this[chindingsSym].substr(1) + '}')), | ||
...bindings | ||
}) | ||
: resetChildingsFormatter, | ||
formatters.log | ||
@@ -196,3 +201,3 @@ ) | ||
if (mixin) { | ||
obj = mixinMergeStrategy(obj, mixin(obj)) | ||
obj = mixinMergeStrategy(obj, mixin(obj, num)) | ||
} | ||
@@ -199,0 +204,0 @@ |
{ | ||
"name": "pino", | ||
"version": "7.8.1", | ||
"version": "7.9.0", | ||
"description": "super fast, all natural json logger", | ||
@@ -5,0 +5,0 @@ "main": "pino.js", |
@@ -34,3 +34,3 @@ // Type definitions for pino 7.0 | ||
type TimeFn = () => string; | ||
type MixinFn = (mergeObject: object) => object; | ||
type MixinFn = (mergeObject: object, level: number) => object; | ||
type MixinMergeStrategyFn = (mergeObject: object, mixinObject: object) => object; | ||
@@ -37,0 +37,0 @@ |
@@ -8,3 +8,3 @@ /* eslint-disable no-eval */ | ||
pino.transport({ | ||
target: 'pino-pretty' | ||
target: 'pino/file' | ||
}) | ||
@@ -11,0 +11,0 @@ ) |
@@ -6,3 +6,3 @@ const pino = require("../../../../"); | ||
pino.transport({ | ||
target: 'pino-pretty' | ||
target: 'pino/file' | ||
}) | ||
@@ -9,0 +9,0 @@ ) |
@@ -204,2 +204,75 @@ 'use strict' | ||
test('Parent bindings in child logger', async ({ match }) => { | ||
const stream = sink() | ||
const logger = pino({ | ||
formatters: { | ||
bindings (bindings) { | ||
return { | ||
...bindings, | ||
process: { | ||
pid: bindings.pid | ||
}, | ||
from: 'parent' | ||
} | ||
} | ||
} | ||
}, stream) | ||
const child = logger.child({ | ||
foo: 'bar' | ||
}) | ||
const childOut = once(stream, 'data') | ||
child.info('hello world') | ||
match(await childOut, { | ||
process: { | ||
pid: process.pid | ||
}, | ||
from: 'parent', | ||
foo: 'bar' | ||
}) | ||
}) | ||
test('Parent bindings in child logger with it\'s own bindings', async ({ match }) => { | ||
const stream = sink() | ||
const logger = pino({ | ||
formatters: { | ||
bindings (bindings) { | ||
return { | ||
process: { | ||
pid: bindings.pid | ||
}, | ||
from: 'parent' | ||
} | ||
} | ||
} | ||
}, stream) | ||
const childWithBindings = logger.child({ | ||
foo: 'bar' | ||
}, { | ||
formatters: { | ||
bindings (bindings) { | ||
return { | ||
...bindings, | ||
from: 'child' | ||
} | ||
} | ||
} | ||
}) | ||
const childWithBindingsOut = once(stream, 'data') | ||
childWithBindings.info('hello world') | ||
match(await childWithBindingsOut, { | ||
process: { | ||
pid: process.pid | ||
}, | ||
foo: 'bar', | ||
from: 'child' | ||
}) | ||
}) | ||
test('Formatters without bindings in child logger', async ({ match }) => { | ||
@@ -206,0 +279,0 @@ const stream = sink() |
@@ -143,1 +143,21 @@ 'use strict' | ||
}) | ||
test('mixin can use level number', async ({ ok, same }) => { | ||
const stream = sink() | ||
const instance = pino({ | ||
mixin (context, num) { | ||
ok(num !== null, 'level should be defined') | ||
ok(num !== undefined, 'level should be defined') | ||
same(num, level) | ||
return Object.assign({ | ||
error: context.message, | ||
stack: context.stack | ||
}) | ||
} | ||
}, stream) | ||
instance.level = name | ||
instance[name]({ | ||
message: '123', | ||
stack: 'stack' | ||
}, 'test') | ||
}) |
@@ -15,2 +15,3 @@ 'use strict' | ||
const { Writable } = stream | ||
const sleep = promisify(setTimeout) | ||
@@ -25,2 +26,3 @@ test('eight million lines', async ({ equal, comment }) => { | ||
await once(child, 'exit') | ||
await sleep(1000) // wait for the file to be written | ||
const toWrite = 8 * 1000000 | ||
@@ -27,0 +29,0 @@ let count = 0 |
'use strict' | ||
const writer = require('flush-write-stream') | ||
const { join } = require('path') | ||
@@ -8,17 +7,6 @@ const { test } = require('tap') | ||
const { once } = require('../helper') | ||
test('when using a custom transport outside node_modules, the first file outside node_modules should be used', async function (t) { | ||
const evalApp = join(__dirname, '../', '/fixtures/eval/index.js') | ||
const child = execa(process.argv[0], [evalApp]) | ||
let actual = '' | ||
child.stdout.pipe(writer((s, enc, cb) => { | ||
actual += s | ||
cb() | ||
})) | ||
await once(child, 'close') | ||
t.match(actual, /done!/) | ||
const { stdout } = await execa(process.argv[0], [evalApp]) | ||
t.match(stdout, /done!/) | ||
}) | ||
@@ -28,13 +16,4 @@ | ||
const evalApp = join(__dirname, '../', '/fixtures/eval/node_modules/2-files.js') | ||
const child = execa(process.argv[0], [evalApp]) | ||
let actual = '' | ||
child.stdout.pipe(writer((s, enc, cb) => { | ||
actual += s | ||
cb() | ||
})) | ||
await once(child, 'close') | ||
t.match(actual, /done!/) | ||
const { stdout } = await execa(process.argv[0], [evalApp]) | ||
t.match(stdout, /done!/) | ||
}) | ||
@@ -44,13 +23,4 @@ | ||
const evalApp = join(__dirname, '../', '/fixtures/eval/node_modules/14-files.js') | ||
const child = execa(process.argv[0], [evalApp]) | ||
let actual = '' | ||
child.stdout.pipe(writer((s, enc, cb) => { | ||
actual += s | ||
cb() | ||
})) | ||
await once(child, 'close') | ||
t.match(actual, /done!/) | ||
const { stdout } = await execa(process.argv[0], [evalApp]) | ||
t.match(stdout, /done!/) | ||
}) |
@@ -51,2 +51,10 @@ import P, { pino } from "../../"; | ||
pino({ | ||
mixin: (context: object) => ({ customName: "unknown", customId: 111 }), | ||
}); | ||
pino({ | ||
mixin: (context: object, level: number) => ({ customName: "unknown", customId: 111 }), | ||
}); | ||
pino({ | ||
redact: { paths: [], censor: "SECRET" }, | ||
@@ -53,0 +61,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
473015
11392