Skip to content

Commit

Permalink
Added onChild callback with tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Diabl0269 committed Aug 31, 2022
1 parent 3388e68 commit 386f46f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
12 changes: 12 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,18 @@ when using the `transport` option. In this case an `Error` will be thrown.
* See [pino.transport()](#pino-transport)
#### `onChild` (Function)
The `onChild` function is a callback that will be called on each creation of a new child and use the child instance as it's first argument.
```js
const parent = require('pino')({ onChild: (instance) => {
// Exceute call back code for each newly created child.
}})
// `onChild` will now be executed with the new child.
parent.child(bindings)
```
<a id="destination"></a>
### `destination` (SonicBoom | WritableStream | String | Object)
Expand Down
2 changes: 1 addition & 1 deletion lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function child (bindings, options) {
instance[chindingsSym] = asChindings(instance, bindings)
const childLevel = options.level || this.level
instance[setLevelSym](childLevel)

this.onChild(instance)
return instance
}

Expand Down
3 changes: 2 additions & 1 deletion lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,9 @@ function createArgsNormalizer (defaultOptions) {
if ('onTerminated' in opts) {
throw Error('The onTerminated option has been removed, use pino.final instead')
}
const { enabled } = opts
const { enabled, onChild } = opts
if (enabled === false) opts.level = 'silent'
if (!onChild) opts.onChild = noop
if (!stream) {
if (!hasBeenTampered(process.stdout)) {
// If process.stdout.fd is undefined, it means that we are running
Expand Down
6 changes: 6 additions & 0 deletions pino.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ interface LoggerExtras<Options = LoggerOptions> extends EventEmitter {
*/
child<ChildOptions extends pino.ChildLoggerOptions>(bindings: pino.Bindings, options?: ChildOptions): pino.Logger<Options & ChildOptions>;

/**
* A callback that will run on each creation of a new child.
* @param child: The newly created child logger instance.
*/
onChild<ChildOptions extends pino.ChildLoggerOptions>(child: pino.Logger<Options & ChildOptions>): void;

/**
* Registers a listener function that is triggered when the level is changed.
* Note: When browserified, this functionality will only be available if the `events` module has been required elsewhere
Expand Down
6 changes: 4 additions & 2 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ function pino (...args) {
formatters,
hooks,
depthLimit,
edgeLimit
edgeLimit,
onChild
} = opts

const stringifySafe = configure({
Expand Down Expand Up @@ -175,7 +176,8 @@ function pino (...args) {
[chindingsSym]: chindings,
[formattersSym]: allFormatters,
[hooksSym]: hooks,
silent: noop
silent: noop,
onChild
})

Object.setPrototypeOf(instance, proto())
Expand Down
10 changes: 10 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,13 @@ test('throws if prettyPrint is passed in as an option', async (t) => {
})
}, new Error('prettyPrint option is no longer supported, see the pino-pretty package (https://github.com/pinojs/pino-pretty)'))
})

test('Should invoke `onChild` with the newly created child', async ({ equal }) => {
let innerChild
const child = pino({
onChild: (instance) => {
innerChild = instance
}
}).child({ foo: 'bar' })
equal(child, innerChild)
})

0 comments on commit 386f46f

Please sign in to comment.