pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 875 forks source link

Custom Levels and Log Level Not Working #1638

Closed ghost closed 1 year ago

ghost commented 1 year ago

When I config the log level and custom levels it doesn't work. here is my code

const pino = require('pino')
const pretty = require('pino-pretty')

const levels = {
  http: 10,
  info: 30,
  debug: 35,
  warn: 40,
  error: 50,
  fatal: 60,
}

const log = pino(pretty({
  colorize: true,
  levelFirst: true,
  translateTime: 'yyyy-dd-mm, h:MM:ss TT',
}, {
  customLevels: levels,
  useOnlyCustomLevels: true,
  level: 'http',
  transport: {
    target: 'pino-pretty'
  },
}))
module.exports = log

When I used the HTTP level it errors log.http('Hello World') Expected Output for HTTP Level [HH:MM:SS.SSS] HTTP (PID): Hello World Output for HTTP Level TypeError: log.http is not a function

Why doesn't this work? Also even with the custom levels not working, the levels themselves aren't working! Even if I do this

const log = pino({ level: "trace" })

log.debug messages still don't show up like the level is still info

ademarsj commented 1 year ago

Hello, i reproduce this and find a way:

First, the pino function used to initialize receives the parameters in order: (options, destinationStream), you used backwards. Then, you pass the pretty twice, one for transporter and one as destinationStream, this will confuse your logs, pino-pretty is a final transporter/stream, he will give you the output, so if you want to add another transporter you will have to use like a transporter but in another way, i recommend create another file, one to each transporter. Finally, I just removed the transporter from pino and concatenated the pino-pretty as destinationStream.

Lastly, pino-pretty has some lacks of documentation so the option you will use is customLevels which is a string, so:

{
  http: 10,
  info: 30,
  debug: 35,
  warn: 40,
  error: 50,
  fatal: 60,
}

will become: "http:10,info:30,debug:35,warn:40,error:50,fatal:60" with the replaces and slice i did (so you don't have to do everything manually), the final code working:

const pino = require('pino');
const pretty = require('pino-pretty');

const levels = {
  http: 10,
  info: 30,
  debug: 35,
  warn: 40,
  error: 50,
  fatal: 60,
}

const jsonLevels = JSON.stringify(levels);
const levelsInString = jsonLevels.replaceAll('"','').slice(0,-1).slice(1)

const log = pino(
  {
    customLevels: levels,
    useOnlyCustomLevels: true,
    level: 'http',
    // transport: {
    //   target: 'pino-pretty'
    // },
  },
  pretty({
  colorize: true,
  levelFirst: true,
  customLevels: levelsInString,
  translateTime: 'yyyy-dd-mm, h:MM:ss TT',
  })
);

log.http('Http test log');
log.debug('DEBUG MESSAGE');

Output: HTTP [2023-06-02, 12:16:57 AM] (22720): Http test log DEBUG [2023-06-02, 12:19:21 AM] (23016): DEBUG MESSAGE

ghost commented 1 year ago

@ademarsj Thank you!

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.