pinojs / pino-pretty

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

Pretty print issue when logging with context #325

Closed tichon29 closed 2 years ago

tichon29 commented 2 years ago

I am using the following npm packages:

and node v14.15.4

The simple code below:

const pino = require('pino');

const Logger = pino({
  prettyPrint: {},
  level: 'debug'
});
const child = Logger.child({ a: 'property' });

const fakeContext = {fakeKey: 'fakeValue'};
child.debug(fakeContext, 'First instance of log');
child.info(fakeContext, 'Second instance of log');

const fakeContext2 = {fakeKey: 'fakeValue'};
child.debug(fakeContext2, 'First instance of log with another context');
child.info(fakeContext2, 'Second instance of log with another context');

generates this data in the console:

First instance of log
    fakeKey: "fakeValue"
    a: "property"
First instance of log
    fakeKey: "fakeValue"
    a: "property"
First instance of log with another context
    fakeKey: "fakeValue"
    a: "property"
First instance of log with another context
    fakeKey: "fakeValue"
    a: "property"

while it should be:

First instance of log
    fakeKey: "fakeValue"
    a: "property"
Second instance of log
    fakeKey: "fakeValue"
    a: "property"
First instance of log with another context
    fakeKey: "fakeValue"
    a: "property"
Second instance of log with another context
    fakeKey: "fakeValue"
    a: "property"

Without prettyPrint, the result is fine:

{"level":20,"time":1649263454522,"a":"property","fakeKey":"fakeValue","msg":"First instance of log"}
{"level":30,"time":1649263454522,"a":"property","fakeKey":"fakeValue","msg":"Second instance of log"}
{"level":20,"time":1649263454522,,"a":"property","fakeKey":"fakeValue","msg":"First instance of log with another context"}
{"level":30,"time":1649263454522,,"a":"property","fakeKey":"fakeValue","msg":"Second instance of log with another context"}

FYI, with pino version 7.6.5, this problem with PrettyPrint does not exist

mcollina commented 2 years ago

looks like a bug here! Would you like to send a PR?

tichon29 commented 2 years ago

@mcollina , looks like the way I was using prettyPrint is deprecated. Using the new recommended way solves my issue:

const Logger = pino({
  level: 'debug',
  transport: {
    target: 'pino-pretty'
  }
});