-
Notifications
You must be signed in to change notification settings - Fork 167
Feature/colorize message #611
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,8 +33,10 @@ function prettifyMessage ({ log, context }) { | |
| levelLabel, | ||
| messageFormat, | ||
| messageKey, | ||
| useOnlyCustomProps | ||
| useOnlyCustomProps, | ||
| colorizeMessage | ||
| } = context | ||
|
|
||
| if (messageFormat && typeof messageFormat === 'string') { | ||
| const parsedMessageFormat = interpretConditionals(messageFormat, log) | ||
|
|
||
|
|
@@ -51,13 +53,28 @@ function prettifyMessage ({ log, context }) { | |
| // Parse nested key access, e.g. `{keyA.subKeyB}`. | ||
| return getPropertyValue(log, p1) || '' | ||
| }) | ||
|
|
||
| if (colorizeMessage) { | ||
| return colorizer.levelColors[getPropertyValue(log, levelKey)](message) | ||
| } | ||
|
Comment on lines
+57
to
+59
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Complementing the comment in Also, using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not understand. The sole purpose of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It actually does, but only in Cyan (see #430 print). I understand your point that this might seem contrary to the library’s design. However, the In const { createColors } = require('colorette')
const availableColors = createColors({ useColor: true })
const { ..., cyan, ... } = availableColors
const colored = {
default: white,
60: bgRed,
....
message: cyan,
...
}
function coloredColorizer (useOnlyCustomProps) {
const newColoredColorizer = colorizeLevel(useOnlyCustomProps)
const customColoredColorizer = function (level, opts) {
return newColoredColorizer(level, colored, opts)
}
customColoredColorizer.message = colored.message
...
}Changing colorette itself wouldn’t make sense, since the library is correctly fulfilling its purpose. The behavior we want to adjust lies within pino-pretty. Modifying the existing factory methods to achieve this would require a broader refactor—essentially introducing middleware logic before calling colorette, which would affect the entire colorization flow. This PR, instead, introduces an way to colorize the message using the same color as the log level, without overriding the default behavior. It preserves the current integration between pino-pretty and colorette, while giving users more flexibility in how messages are colorized—avoiding design breaks rather than causing them. |
||
|
|
||
| return colorizer.message(message) | ||
| } | ||
| if (messageFormat && typeof messageFormat === 'function') { | ||
| const msg = messageFormat(log, messageKey, levelLabel, { colors: colorizer.colors }) | ||
|
|
||
| if (colorizeMessage) { | ||
| return colorizer.levelColors[getPropertyValue(log, levelKey)](msg) | ||
| } | ||
|
|
||
| return colorizer.message(msg) | ||
| } | ||
| if (messageKey in log === false) return undefined | ||
| if (typeof log[messageKey] !== 'string' && typeof log[messageKey] !== 'number' && typeof log[messageKey] !== 'boolean') return undefined | ||
|
|
||
| if (colorizeMessage) { | ||
| return colorizer.levelColors[getPropertyValue(log, levelKey)](log[messageKey]) | ||
| } | ||
|
|
||
| return colorizer.message(log[messageKey]) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
.levelColorsbeing set to the hash of colors on all three colorizers? The other properties, e.g..message, are set to the color that field will be colorized to.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To preserve all color levels and make use of them,
.messagewill remain cyan during tests.The colorizer will initially select one of the two constants:
colored: message as cyan
or
plain: nocolor;
e.g: