pinojs / pino-pretty

🌲Basic prettifier for Pino log lines
MIT License
1.27k stars 150 forks source link

LevelName whitespace alignment issue #489

Closed JohnPolansky closed 8 months ago

JohnPolansky commented 10 months ago

I'm using the following packages:

    "pino": "^8.17.2",
    "pino-pretty": "^10.3.1",

When I print out any messages using the colorize() option it appears that the message alignment is off image

As you can see the INFO/WARN are throwing off messages which makes it a bit harder to read log messages. After searching various older posts I found a reference to this same issue back in 2021 that indicated this is an issue and it was fixed. Has something changed or am I missing a feature? https://github.com/pinojs/pino-pretty/pull/141

I also tried creating my own transport to padEnd() the log level, but this doesn't appear to work as below

import pino from 'pino-pretty'
import { colorizerFactory } from 'pino-pretty'
const levelColorize = colorizerFactory(true)
const levelPrettifier = logLevel => `${levelColorize(logLevel).padEnd(5)}`

export default (opts) => {
    return pino({
        ...opts,
        messageFormat: (log, messageKey) => `hello ${log[messageKey]}`
        customPrettifiers: {
            level: levelPrettifier
        }
    })
}

Here is my full logger.js for review:

import pino from 'pino';

//Create a logging instance
export const logger = pino({
  enabled: process.env.LOG_ENABLED === undefined ? true : process.env.LOG_ENABLED === 'true',
  transport: {
    // ...(process.env.LOG_PRETTY === true) && {'target': 'pino-pretty'},
    target: process.env.LOG_PRETTY === 'true' ? 'pino-pretty' : 'pino/file',
    // target: './pino-pretty-transport.js',
    options: {
      colorize: true,
      colorizeObjects: true,
      singleLine: true,
      ignore: 'pid,hostname',
    }
  },
  base: {},
  formatters: {
    level: (label) => {
      return { level: label };
    },
  },
  level: process.env.LOG_LEVEL || 'info',
  name: process.env.LOGGER_NAME,
  redact: {
    paths: ['token', 'password', 'uri'],
  },
  timestamp: pino.stdTimeFunctions.isoTime,
});

Thanks in advance for the help and the great tool.

mcollina commented 10 months ago

Thanks for reporting. I don't exactly remember what happened.

@jsumners did you recall what happened on this one?

jsumners commented 10 months ago

https://github.com/pinojs/pino-pretty/issues/140 is the last time this issue was discussed to the best of my recollection. In short, we've had two attempts performed at introducing the requested alignment. It seems it is not an easy feature to implement. I would rather not keep churning on the issue, but whomever feels strongly about it is welcome to work on it.

FoxxMD commented 8 months ago

@JohnPolansky in pino-pretty v11.0.0 you now have access to the log level label and alignment in now possible. An example from my PR:

{
  customPrettifiers: {
        level: (logLevel, key, log, { label, labelColorized }) => {
            // pad to fix alignment
            // assuming longest level is 5 characters long
            // and may be colorized
            const paddedLabel = label.padEnd(5)
            if(labelColorized !== label) {
                const padDiff = paddedLabel.length - label.length;
                return labelColorized.padEnd(labelColorized.length + padDiff);
            }
            return paddedLabel;
        }
    },
}