Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ const prettifyQuery = value => {
}
```

```ts
// In TypeScript you have to use a Map
{
customPrettifiers: new Map([
['query', prettifyQuery]
])
}
```

All prettifiers use this function signature:

```js
Expand Down
18 changes: 8 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,25 @@ declare namespace PinoPretty {
mkdir?: boolean;
/**
* Provides the ability to add a custom prettify function for specific log properties.
* `customPrettifiers` is an object, where keys are log properties that will be prettified
* `customPrettifiers` is a Map, where keys are log properties that will be prettified
* and value is the prettify function itself.
* For example, if a log line contains a query property, you can specify a prettifier for it:
* @default {}
* @default new Map()
*
* @example
* ```typescript
* {
* customPrettifiers: {
* query: prettifyQuery
* }
* customPrettifiers: new Map([
* ['query', prettifyQuery]
* ])
* }
* //...
* const prettifyQuery = value => {
* // do some prettify magic
* }
* ```
*/
customPrettifiers?: Record<string, Prettifier> &
{
level?: Prettifier<LevelPrettifierExtras>
};
customPrettifiers?: CustomPrettifiers;
/**
* Change the level names and values to an user custom preset.
*
Expand Down Expand Up @@ -218,6 +215,7 @@ declare namespace PinoPretty {

function build(options: PrettyOptions): PrettyStream;

type CustomPrettifiers = Map<'level', Prettifier<LevelPrettifierExtras>> & Map<string, Prettifier>
type Prettifier<T = object> = (inputData: string | object, key: string, log: object, extras: PrettifierExtras<T>) => string;
type PrettifierExtras<T = object> = {colors: Colorette.Colorette} & T;
type LevelPrettifierExtras = {label: string, labelColorized: string}
Expand All @@ -228,7 +226,7 @@ declare namespace PinoPretty {
type Build = typeof build;
type isColorSupported = typeof Colorette.isColorSupported;

export { build, PinoPretty, PrettyOptions, PrettyStream, colorizerFactory, prettyFactory, isColorSupported };
export { build, PinoPretty, PrettyOptions, CustomPrettifiers, PrettyStream, colorizerFactory, prettyFactory, isColorSupported };
}

export = PinoPretty;
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ const defaultOptions = {
* @returns {LogPrettifierFunc}
*/
function prettyFactory (options) {
if (options.customPrettifiers instanceof Map) {
options.customPrettifiers = Object.fromEntries(options.customPrettifiers)
}
const context = parseFactoryOptions(Object.assign({}, defaultOptions, options))
return pretty.bind({ ...context, context })
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = {
* + `name`
* + `caller`
*
* @typedef {Object.<string, CustomPrettifierFunc>} CustomPrettifiers
* @typedef {Object.<string, CustomPrettifierFunc>|Map<string, CustomPrettifierFunc>} CustomPrettifiers
*/

/**
Expand Down
21 changes: 21 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ process.removeAllListeners('warning')
function prettyFactory (opts) {
if (!opts) {
opts = { colorize: false }
} else if (opts instanceof Map && !opts.has('colorize')) {
opts.set('colorize', false)
} else if (!Object.prototype.hasOwnProperty.call(opts, 'colorize')) {
opts.colorize = false
}
Expand Down Expand Up @@ -257,6 +259,25 @@ test('basic prettifier tests', (t) => {
log.info({ msg: 'foo' })
})

t.test('can use a Map customPrettifier', (t) => {
t.plan(1)
const customPrettifiers = new Map([
['level', () => 'LEVEL: bar']
])
const pretty = prettyFactory({ customPrettifiers })
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${formattedEpoch}] LEVEL: bar (${pid}): foo\n`
)
cb()
}
}))
log.info({ msg: 'foo' })
})

t.test('can use a customPrettifier on different-level-key output', (t) => {
t.plan(1)
const customPrettifiers = {
Expand Down
23 changes: 15 additions & 8 deletions test/types/pino-pretty.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import pretty from "../../";
import PinoPretty, {
PinoPretty as PinoPrettyNamed,
PrettyOptions,
CustomPrettifiers,
colorizerFactory,
prettyFactory
} from "../../";
Expand All @@ -13,6 +14,19 @@ import PinoPrettyCjsImport = require("../../");
import PrettyStream = PinoPretty.PrettyStream;
const PinoPrettyCjs = require("../../");

const customPrettifiers: CustomPrettifiers = new Map();
customPrettifiers.set("key", (value) => {
return value.toString().toUpperCase();
});
customPrettifiers.set(
"level",
(level, levelKey, log, { label, labelColorized, colors }) => {
return level.toString();
}
);
customPrettifiers.set("foo", (value, key, log, { colors }) => {
return value.toString();
});
const options: PinoPretty.PrettyOptions = {
colorize: true,
crlf: false,
Expand All @@ -29,14 +43,7 @@ const options: PinoPretty.PrettyOptions = {
minimumLevel: "trace",
translateTime: "UTC:h:MM:ss TT Z",
singleLine: false,
customPrettifiers: {
key: (value) => {
return value.toString().toUpperCase();
},
level: (level, label, colorized) => {
return level.toString();
}
},
customPrettifiers,
customLevels: 'verbose:5',
customColors: 'default:white,verbose:gray',
sync: false,
Expand Down