Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass logger instance as an argument in level-change event #1576

Merged
merged 2 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/api.md
Expand Up @@ -1003,12 +1003,13 @@ The logger instance is also an [`EventEmitter ⇗`](https://nodejs.org/dist/late

A listener function can be attached to a logger via the `level-change` event

The listener is passed four arguments:
The listener is passed five arguments:

* `levelLabel` – the new level string, e.g `trace`
* `levelValue` – the new level number, e.g `10`
* `previousLevelLabel` – the prior level string, e.g `info`
* `previousLevelValue` – the prior level number, e.g `30`
* `logger` – the logger instance from which the event originated

```js
const logger = require('pino')()
Expand All @@ -1023,8 +1024,8 @@ fire a `level-change` event. These events can be ignored by writing an event han

```js
const logger = require('pino')()
logger.on('level-change', function (lvl, val, prevLvl, prevVal) {
if (logger !== this) {
logger.on('level-change', function (lvl, val, prevLvl, prevVal, instance) {
if (logger !== instance) {
return
}
console.log('%s (%d) was changed to %s (%d)', prevLvl, prevVal, lvl, val)
Expand Down
3 changes: 2 additions & 1 deletion lib/levels.js
Expand Up @@ -105,7 +105,8 @@ function setLevel (level) {
level,
levelVal,
labels[preLevelVal],
preLevelVal
preLevelVal,
this
)
}

Expand Down
15 changes: 8 additions & 7 deletions pino.d.ts
Expand Up @@ -96,12 +96,12 @@ interface LoggerExtras<Options = LoggerOptions> extends EventEmitter {
* @param event: only ever fires the `'level-change'` event
* @param listener: The listener is passed four arguments: `levelLabel`, `levelValue`, `previousLevelLabel`, `previousLevelValue`.
*/
on(event: "level-change", listener: pino.LevelChangeEventListener): this;
addListener(event: "level-change", listener: pino.LevelChangeEventListener): this;
once(event: "level-change", listener: pino.LevelChangeEventListener): this;
prependListener(event: "level-change", listener: pino.LevelChangeEventListener): this;
prependOnceListener(event: "level-change", listener: pino.LevelChangeEventListener): this;
removeListener(event: "level-change", listener: pino.LevelChangeEventListener): this;
on<Opts = Options>(event: "level-change", listener: pino.LevelChangeEventListener<Opts>): this;
addListener<Opts = Options>(event: "level-change", listener: pino.LevelChangeEventListener<Opts>): this;
once<Opts = Options>(event: "level-change", listener: pino.LevelChangeEventListener<Opts>): this;
prependListener<Opts = Options>(event: "level-change", listener: pino.LevelChangeEventListener<Opts>): this;
prependOnceListener<Opts = Options>(event: "level-change", listener: pino.LevelChangeEventListener<Opts>): this;
removeListener<Opts = Options>(event: "level-change", listener: pino.LevelChangeEventListener<Opts>): this;

/**
* A utility method for determining if a given log level will write to the destination.
Expand Down Expand Up @@ -223,11 +223,12 @@ declare namespace pino {
type SerializerFn = (value: any) => any;
type WriteFn = (o: object) => void;

type LevelChangeEventListener = (
type LevelChangeEventListener<Options = LoggerOptions> = (
lvl: LevelWithSilent | string,
val: number,
prevLvl: LevelWithSilent | string,
prevVal: number,
logger: Logger<Options>
) => void;

type LogDescriptor = Record<string, any>;
Expand Down
9 changes: 8 additions & 1 deletion test/levels.test.js
Expand Up @@ -96,11 +96,12 @@ test('set the level via exported pino function', async ({ equal }) => {

test('level-change event', async ({ equal }) => {
const instance = pino()
function handle (lvl, val, prevLvl, prevVal) {
function handle (lvl, val, prevLvl, prevVal, logger) {
equal(lvl, 'trace')
equal(val, 10)
equal(prevLvl, 'info')
equal(prevVal, 30)
equal(logger, instance)
}
instance.on('level-change', handle)
instance.level = 'trace'
Expand All @@ -125,6 +126,12 @@ test('level-change event', async ({ equal }) => {
instance.level = 'info'

equal(count, 6)

instance.once('level-change', (lvl, val, prevLvl, prevVal, logger) => equal(logger, instance))
instance.level = 'info'
const child = instance.child({})
instance.once('level-change', (lvl, val, prevLvl, prevVal, logger) => equal(logger, child))
child.level = 'trace'
})

test('enable', async ({ fail }) => {
Expand Down
7 changes: 6 additions & 1 deletion test/types/pino.test-d.ts
Expand Up @@ -153,7 +153,7 @@ if (log.levelVal === 30) {
const listener = (lvl: any, val: any, prevLvl: any, prevVal: any) => {
console.log(lvl, val, prevLvl, prevVal);
};
log.on("level-change", (lvl, val, prevLvl, prevVal) => {
log.on("level-change", (lvl, val, prevLvl, prevVal, logger) => {
console.log(lvl, val, prevLvl, prevVal);
});
log.level = "trace";
Expand Down Expand Up @@ -303,6 +303,10 @@ log3.level = 'myLevel'
log3.myLevel('')
log3.child({}).myLevel('')

log3.on('level-change', (lvl, val, prevLvl, prevVal, instance) => {
instance.myLevel('foo');
});

const clog3 = log3.child({}, { customLevels: { childLevel: 120 } })
// child inherit parant
clog3.myLevel('')
Expand All @@ -320,3 +324,4 @@ const withChildCallback = pino({
onChild: (child: Logger) => {}
})
withChildCallback.onChild = (child: Logger) => {}