pino-pretty: Log level incorrectly shows up as USERLVL when `useLevelLabels: true`

I created pino with option useLevelLabels: true. When formatting via pino-pretty those log lines show up as

[2019-04-23 13:56:29.560 +0000] USERLVL (runScript): Request did succeed

The log line itself cpontains

{
  "level": "info",
  "time": 1556027789560,
  "name": "runScript",
  "msg": "Request did succeed",
  "v": 1
}

Given that useLevelLabels is a feature built into pino, pino-pretty should be able to understand their corresponding string based levels.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 11
  • Comments: 24 (11 by maintainers)

Most upvoted comments

Is there currently any way to work around this? I am currently using the RFC5424 Syslog levels https://tools.ietf.org/html/rfc5424 for more granular logging and the ones that are not defined by pino itself in https://github.com/pinojs/pino-pretty/blob/master/lib/constants.js#L12-L20 just end up showing as USERLVL which is rather unfortunate.

const log = pino({
    base: null,
    customLevels: {
        emergency: 0,
        alert: 1,
        critical: 2,
        error: 3,
        warning: 4,
        notice: 5,
        info: 6,
        debug: 7,
    },
    level: "emergency",
    useLevelLabels: true,
    useOnlyCustomLevels: true,
    safe: true,
});

log.emergency("Hello World");
log.alert("Hello World");
log.critical("Hello World");
log.error("Hello World");
log.warning("Hello World");
log.notice("Hello World");
log.info("Hello World");
log.debug("Hello World");
[2019-09-02 02:45:57.300] USERLVL: Hello World
[2019-09-02 02:45:57.308] USERLVL: Hello World
[2019-09-02 02:45:57.309] USERLVL: Hello World
[2019-09-02 02:45:57.309] ERROR: Hello World
[2019-09-02 02:45:57.310] USERLVL: Hello World
[2019-09-02 02:45:57.310] USERLVL: Hello World
[2019-09-02 02:45:57.311] INFO : Hello World
[2019-09-02 02:45:57.311] DEBUG: Hello World

@rodrigo-oleria I’ve successfully addressed a similar issue with Pino and pino-pretty by defining custom log levels. Here’s the TypeScript code that worked for me:

const levels = {
  fatal: 60,
  error: 50,
  warn: 40,
  info: 30,
  custom: 26, // custom level 1
  ascii: 25, // custom level 2
  debug: 20,
  trace: 10,
}

export const logger = pino({
  level: process.env.LOG_LEVER || 'ascii',
  customLevels: levels,
  useOnlyCustomLevels: true,

  transport: {
    target: 'pino-pretty',
    options: {
      customLevels: 'custom:26,ascii:25',     // split by ','
      customColors: 'custom:cyan,ascii:gray', // split by ','
      useOnlyCustomProps: false,
    },
  },
})

While the lack of type safety in this configuration might be inconvenient, the infrequency of changes to the logger’s setup makes this a manageable aspect for me.

The key was ensuring the custom levels and their colors were accurately defined and integrated into both Pino and the pino-pretty transport options. This setup has been effective in my use case.

CleanShot 2023-12-01 at 22 33 31

@55Cancri They’ve been waiting for your submission, clearly! Would you like to send a PR? https://github.com/pinojs/pino-pretty/pull/317 is not covering what you need?

Ah got it. Confirming pino-pretty@next works. It’s worth noting that I installed pino not pino@next so it’d be great if pino-pretty can be synced with pino version wise.

maintain the alignment for level labels

What I’m saying is we don’t need to maintain anything beyond the standard ones defined (10,20,30,40,50 + trace,debug,info,warn,error). The rest just display exactly as USERLVL today except instead of using the word USERLVL it would be as the level themselves.