Skip to content

Commit 553c66b

Browse files
Diabl0269jsumners
andauthoredSep 5, 2022
Added onChild callback with tests and documentation (#1541)
* Added `onChild` callback with tests and documentation * Update `onChild` type to also be in options and added types tests * Apply suggestions from code review Co-authored-by: James Sumners <james@sumners.email> * updated documentation * Updated documentation. Expanded `onChild` documentation to mention the function doesn't handle errors Co-authored-by: James Sumners <james@sumners.email>
1 parent af3285f commit 553c66b

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed
 

‎docs/api.md

+13
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,19 @@ when using the `transport` option. In this case an `Error` will be thrown.
507507
508508
* See [pino.transport()](#pino-transport)
509509
510+
#### `onChild` (Function)
511+
512+
The `onChild` function is a synchronous callback that will be called on each creation of a new child, passing the child instance as its first argument.
513+
Any error thrown inside the callback will be uncaught and should be handled inside the callback.
514+
```js
515+
const parent = require('pino')({ onChild: (instance) => {
516+
// Exceute call back code for each newly created child.
517+
}})
518+
// `onChild` will now be executed with the new child.
519+
parent.child(bindings)
520+
```
521+
522+
510523
<a id="destination"></a>
511524
### `destination` (SonicBoom | WritableStream | String | Object)
512525

‎lib/proto.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function child (bindings, options) {
141141
instance[chindingsSym] = asChindings(instance, bindings)
142142
const childLevel = options.level || this.level
143143
instance[setLevelSym](childLevel)
144-
144+
this.onChild(instance)
145145
return instance
146146
}
147147

‎lib/tools.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,9 @@ function createArgsNormalizer (defaultOptions) {
298298
throw new Error('prettyPrint option is no longer supported, see the pino-pretty package (https://github.com/pinojs/pino-pretty)')
299299
}
300300

301-
const { enabled } = opts
301+
const { enabled, onChild } = opts
302302
if (enabled === false) opts.level = 'silent'
303+
if (!onChild) opts.onChild = noop
303304
if (!stream) {
304305
if (!hasBeenTampered(process.stdout)) {
305306
// If process.stdout.fd is undefined, it means that we are running

‎pino.d.ts

+16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ type MixinMergeStrategyFn = (mergeObject: object, mixinObject: object) => object
3636

3737
type CustomLevelLogger<Options> = Options extends { customLevels: Record<string, number> } ? Record<keyof Options["customLevels"], LogFn> : Record<never, LogFn>
3838

39+
/**
40+
* A synchronous callback that will run on each creation of a new child.
41+
* @param child: The newly created child logger instance.
42+
*/
43+
type OnChildCallback<Options = LoggerOptions> = <ChildOptions extends pino.ChildLoggerOptions>(child: pino.Logger<Options & ChildOptions>) => void
44+
3945
interface redactOptions {
4046
paths: string[];
4147
censor?: string | ((value: any, path: string[]) => any);
@@ -79,6 +85,11 @@ interface LoggerExtras<Options = LoggerOptions> extends EventEmitter {
7985
*/
8086
child<ChildOptions extends pino.ChildLoggerOptions>(bindings: pino.Bindings, options?: ChildOptions): pino.Logger<Options & ChildOptions>;
8187

88+
/**
89+
* This can be used to modify the callback function on creation of a new child.
90+
*/
91+
onChild: OnChildCallback<Options>;
92+
8293
/**
8394
* Registers a listener function that is triggered when the level is changed.
8495
* Note: When browserified, this functionality will only be available if the `events` module has been required elsewhere
@@ -601,6 +612,11 @@ declare namespace pino {
601612
* Stringification limit of properties/elements when logging a specific object/array with circular references. Default: `100`.
602613
*/
603614
edgeLimit?: number
615+
616+
/**
617+
* Optional child creation callback.
618+
*/
619+
onChild?: OnChildCallback;
604620
}
605621

606622
interface ChildLoggerOptions {

‎pino.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ function pino (...args) {
102102
formatters,
103103
hooks,
104104
depthLimit,
105-
edgeLimit
105+
edgeLimit,
106+
onChild
106107
} = opts
107108

108109
const stringifySafe = configure({
@@ -175,7 +176,8 @@ function pino (...args) {
175176
[chindingsSym]: chindings,
176177
[formattersSym]: allFormatters,
177178
[hooksSym]: hooks,
178-
silent: noop
179+
silent: noop,
180+
onChild
179181
})
180182

181183
Object.setPrototypeOf(instance, proto())

‎test/basic.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -725,3 +725,13 @@ test('throws if prettyPrint is passed in as an option', async (t) => {
725725
})
726726
}, new Error('prettyPrint option is no longer supported, see the pino-pretty package (https://github.com/pinojs/pino-pretty)'))
727727
})
728+
729+
test('Should invoke `onChild` with the newly created child', async ({ equal }) => {
730+
let innerChild
731+
const child = pino({
732+
onChild: (instance) => {
733+
innerChild = instance
734+
}
735+
}).child({ foo: 'bar' })
736+
equal(child, innerChild)
737+
})

‎test/types/pino.test-d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,8 @@ cclog3.myLevel('')
340340
cclog3.childLevel('')
341341
// child itself
342342
cclog3.childLevel2('')
343+
344+
const withChildCallback = pino({
345+
onChild: (child: Logger) => {}
346+
})
347+
withChildCallback.onChild = (child: Logger) => {}

0 commit comments

Comments
 (0)
Please sign in to comment.