Skip to content

Commit

Permalink
feat: Provide colorette object to messageFormat
Browse files Browse the repository at this point in the history
Enables users to use available colors based on `colorize` context of the pino-pretty instance
  • Loading branch information
FoxxMD committed Feb 23, 2024
1 parent b67b7c4 commit ae37560
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Readme.md
Expand Up @@ -356,9 +356,11 @@ This option can also be defined as a `function` with this prototype:

```js
{
messageFormat: (log, messageKey, levelLabel) => {
messageFormat: (log, messageKey, levelLabel, colors) => {
// do some log message customization
return customized_message;
//
// `colors` is a Colorette object with colors enabled based on `colorize` option
return `This is a ${color.red('colorized')}, custom message: ${log[messageKey]}`;
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Expand Up @@ -10,6 +10,7 @@ import { Transform } from 'stream';
import { OnUnknown } from 'pino-abstract-transport';
// @ts-ignore fall back to any if pino is not available, i.e. when running pino tests
import { DestinationStream, Level } from 'pino';
import * as Colorette from "colorette";

type LogDescriptor = Record<string, unknown>;

Expand Down Expand Up @@ -205,7 +206,7 @@ declare function build(options: PrettyOptions_): PinoPretty.PrettyStream;

declare namespace PinoPretty {
type Prettifier = (inputData: string | object) => string;
type MessageFormatFunc = (log: LogDescriptor, messageKey: string, levelLabel: string) => string;
type MessageFormatFunc = (log: LogDescriptor, messageKey: string, levelLabel: string, colors: Colorette.Colorette) => string;
type PrettyOptions = PrettyOptions_;
type PrettyStream = Transform & OnUnknown;
type ColorizerFactory = typeof colorizerFactory;
Expand Down
3 changes: 3 additions & 0 deletions lib/colors.js
Expand Up @@ -67,6 +67,7 @@ function plainColorizer (useOnlyCustomProps) {
}
customColoredColorizer.message = plain.message
customColoredColorizer.greyMessage = plain.greyMessage
customColoredColorizer.colors = createColors({ useColor: false })
return customColoredColorizer
}

Expand All @@ -77,6 +78,7 @@ function coloredColorizer (useOnlyCustomProps) {
}
customColoredColorizer.message = colored.message
customColoredColorizer.greyMessage = colored.greyMessage
customColoredColorizer.colors = availableColors
return customColoredColorizer
}

Expand Down Expand Up @@ -105,6 +107,7 @@ function customColoredColorizerFactory (customColors, useOnlyCustomProps) {
* recognized.
* @property {function} message Accepts one string parameter that will be
* colorized to a predefined color.
* @property {Colorette.Colorette} colors Available color functions based on `useColor` (or `colorize`) context
*/

/**
Expand Down
1 change: 1 addition & 0 deletions lib/utils/index.js
Expand Up @@ -90,6 +90,7 @@ module.exports = {
* contains the log message.
* @param {string} levelLabel The name of the key in the `log` object that
* contains the log level name.
* @param {Colorette.Colorette} colors Available color functions based on `colorize` context
* @returns {string}
*
* @example
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/prettify-message.js
Expand Up @@ -54,7 +54,7 @@ function prettifyMessage ({ log, context }) {
return colorizer.message(message)
}
if (messageFormat && typeof messageFormat === 'function') {
const msg = messageFormat(log, messageKey, levelLabel)
const msg = messageFormat(log, messageKey, levelLabel, colorizer.colors)
return colorizer.message(msg)
}
if (messageKey in log === false) return undefined
Expand Down
34 changes: 34 additions & 0 deletions lib/utils/prettify-message.test.js
Expand Up @@ -185,3 +185,37 @@ tap.test('`messageFormat` supports function definition', async t => {
})
t.equal(str, '--> localhost/test')
})

tap.test('`messageFormat` supports function definition with colorizer object', async t => {
const colorizer = getColorizer(true)
const str = prettifyMessage({
log: { level: 30, request: { url: 'localhost/test' }, msg: 'incoming request' },
context: {
...context,
colorizer,
messageFormat: (log, messageKey, levelLabel, colors) => {
let msg = log[messageKey]
if (msg === 'incoming request') msg = `--> ${colors.red(log.request.url)}`
return msg
}
}
})
t.equal(str, '\u001B[36m--> \u001B[31mlocalhost/test\u001B[36m\u001B[39m')
})

tap.test('`messageFormat` supports function definition with colorizer object when no color is supported', async t => {
const colorizer = getColorizer(false)
const str = prettifyMessage({
log: { level: 30, request: { url: 'localhost/test' }, msg: 'incoming request' },
context: {
...context,
colorizer,
messageFormat: (log, messageKey, levelLabel, colors) => {
let msg = log[messageKey]
if (msg === 'incoming request') msg = `--> ${colors.red(log.request.url)}`
return msg
}
}
})
t.equal(str, '--> localhost/test')
})

0 comments on commit ae37560

Please sign in to comment.