diff --git a/Readme.md b/Readme.md index 2f35e7f2..d2174235 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/index.d.ts b/index.d.ts index 2c4c5c8b..c488e396 100644 --- a/index.d.ts +++ b/index.d.ts @@ -165,17 +165,17 @@ 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 => { @@ -183,10 +183,7 @@ declare namespace PinoPretty { * } * ``` */ - customPrettifiers?: Record & - { - level?: Prettifier - }; + customPrettifiers?: CustomPrettifiers; /** * Change the level names and values to an user custom preset. * @@ -218,6 +215,7 @@ declare namespace PinoPretty { function build(options: PrettyOptions): PrettyStream; + type CustomPrettifiers = Map<'level', Prettifier> & Map type Prettifier = (inputData: string | object, key: string, log: object, extras: PrettifierExtras) => string; type PrettifierExtras = {colors: Colorette.Colorette} & T; type LevelPrettifierExtras = {label: string, labelColorized: string} @@ -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; diff --git a/index.js b/index.js index 528b2b0c..a8aaadc3 100644 --- a/index.js +++ b/index.js @@ -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 }) } diff --git a/lib/utils/index.js b/lib/utils/index.js index e083d737..259ccd92 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -52,7 +52,7 @@ module.exports = { * + `name` * + `caller` * - * @typedef {Object.} CustomPrettifiers + * @typedef {Object.|Map} CustomPrettifiers */ /** diff --git a/test/basic.test.js b/test/basic.test.js index e2a58304..c5f61bb9 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -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 } @@ -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 = { diff --git a/test/types/pino-pretty.test-d.ts b/test/types/pino-pretty.test-d.ts index 9f2acd71..b724b046 100644 --- a/test/types/pino-pretty.test-d.ts +++ b/test/types/pino-pretty.test-d.ts @@ -4,6 +4,7 @@ import pretty from "../../"; import PinoPretty, { PinoPretty as PinoPrettyNamed, PrettyOptions, + CustomPrettifiers, colorizerFactory, prettyFactory } from "../../"; @@ -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, @@ -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,