pinojs / pino-pretty

🌲Basic prettifier for Pino log lines
MIT License
1.22k stars 147 forks source link

feat: Add context to level customPrettifier #493

Closed FoxxMD closed 6 months ago

FoxxMD commented 6 months ago

Currently the customPrettifier for level only returns the numeric level value (if levelKey is not used) which means that anyone who wants to modify value returned has to re-implement the levels pino-pretty already has access to as well as calculate the label to use, which pino-pretty also already does.

This PR include both the "final" log label as well as the colorized output, if applicable, to level prettifier function to give users more control over level output.

It has the additional benefit of enabling users to handle their own level alignment (#489 #140) since they have access to the final string values for level. As example implementation for aligning with spaces:


{
  customPrettifiers: {
        level: (out, label, colorized) => {
            // pad to fix alignment
            // assuming longest level is VERBOSE
            // and may be colorized
            const paddedLabel = label.padEnd(8)
            if(colorized !== label) {
                const padDiff = paddedLabel.length - label.length;
                return colorized.padEnd(colorized.length + padDiff);
            }
            return paddedLabel;
        }
    },
}
FoxxMD commented 6 months ago

The only thing missing is that the Typescript typings for all customPrettifiers use Record<string, (inputData: string | object) => string> so TS reports errors when the end user uses level: (out, label, colorized) => { ... and im not sure how to address this.

FoxxMD commented 6 months ago

Modified typings...it should work now

FoxxMD commented 6 months ago

Related to #494 -- providing colors to all customPrettifier funcs would add another argument to this function signature which I'm not super jazzed about.

If I can get feedback on #494 and this cohesively -- or if ya'll would prefer I close both of these and open a new PR with everything combined -- let me know. But I'm thinking:

Change signature for customPrettifier to be (value: string | object, extras: object) so that all these additional variables can be destructured from the second arg to avoid requiring the user to know argument order for multiple signatures.

EDIT: A combined branch example of providing colors to all prettifiers -- see usage under customPrettifiers section in readme https://github.com/FoxxMD/pino-pretty/tree/additionalFunctionality?tab=readme-ov-file#options

mcollina commented 6 months ago

@jsumners could you take a look?

FoxxMD commented 6 months ago

Which part @mcollina ? This PR or the changes I mentioned above in additionalFunctionality branch? https://github.com/pinojs/pino-pretty/pull/493#issuecomment-1964469268

mcollina commented 6 months ago

@FoxxMD sorry I don't understand the question.

FoxxMD commented 6 months ago

Please...read my comment above and respond to it. https://github.com/pinojs/pino-pretty/pull/493#issuecomment-1964469268

For these changes and #494, after I opened these PRs, I found the signatures to be unwiedly

providing colors to all customPrettifier funcs would add another argument

I built out another branch, additionalFunctionality that includes functionally the same changes as this and #494 but with a more normalized signature for both to address this.

Take a look at the customPrettier section on the readme in the additionalFunctionality branch or the diff to see what changes I made.

Per my comment above, the question:

Can I get feedback on #494 and this cohesively -- or would ya'll prefer I close both of these and open a new PR with everything combined (from the additionalFuntionality branch)

"lgtm" reads as if you didn't see this.

mcollina commented 6 months ago

I missed that. Please send one PR thanks.