Skip to content

Commit

Permalink
add new streamWrite hook (#2105)
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim authored Dec 14, 2024
1 parent 1bff08a commit bb1f89d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
17 changes: 17 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,23 @@ const hooks = {
}
```


<a id="streamWrite"></a>
##### `streamWrite`

Allows for manipulating the _stringified_ JSON log data just before writing to various transports.

The method receives the stringified JSON and must return valid stringified JSON.

For example:
```js
const hooks = {
streamWrite (s) {
return s.replaceAll('sensitive-api-key', 'XXX')
}
}
```

<a id=opt-formatters></a>
#### `formatters` (Object)

Expand Down
6 changes: 4 additions & 2 deletions lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const {
stringifySym,
formatOptsSym,
stringifiersSym,
msgPrefixSym
msgPrefixSym,
hooksSym
} = require('./symbols')
const {
getLevel,
Expand Down Expand Up @@ -185,6 +186,7 @@ function write (_obj, msg, num) {
const messageKey = this[messageKeySym]
const mixinMergeStrategy = this[mixinMergeStrategySym] || defaultMixinMergeStrategy
let obj
const streamWriteHook = this[hooksSym].streamWrite

if (_obj === undefined || _obj === null) {
obj = {}
Expand Down Expand Up @@ -214,7 +216,7 @@ function write (_obj, msg, num) {
stream.lastTime = t.slice(this[timeSliceIndexSym])
stream.lastLogger = this // for child loggers
}
stream.write(s)
stream.write(streamWriteHook ? streamWriteHook(s) : s)
}

function noop () {}
Expand Down
6 changes: 6 additions & 0 deletions pino.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,12 @@ declare namespace pino {
* using apply, like so: method.apply(this, newArgumentsArray).
*/
logMethod?: (this: Logger, args: Parameters<LogFn>, method: LogFn, level: number) => void;

/**
* Allows for manipulating the stringified JSON log output just before writing to various transports.
* This function must return a string and must be valid JSON.
*/
streamWrite?: (s: string) => string;
};

/**
Expand Down
3 changes: 2 additions & 1 deletion pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const defaultOptions = {
}
}),
hooks: {
logMethod: undefined
logMethod: undefined,
streamWrite: undefined
},
timestamp: epochTime,
name: undefined,
Expand Down
21 changes: 21 additions & 0 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,24 @@ tap.test('log method hook', t => {

t.end()
})

tap.test('streamWrite hook', t => {
t.test('gets invoked', async t => {
t.plan(1)

const stream = sink()
const logger = pino({
hooks: {
streamWrite (s) {
return s.replaceAll('redact-me', 'XXX')
}
}
}, stream)

const o = once(stream, 'data')
logger.info('hide redact-me in this string')
t.match(await o, { msg: 'hide XXX in this string' })
})

t.end()
})
4 changes: 4 additions & 0 deletions test/types/pino.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ const withHooks = pino({
expectType<pino.Logger>(this);
return method.apply(this, args);
},
streamWrite(s) {
expectType<string>(s);
return s.replaceAll('secret-key', 'xxx');
},
},
});

Expand Down

0 comments on commit bb1f89d

Please sign in to comment.