pinojs / pino-pretty

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

`sync` doesn't work #504

Open inwenis opened 6 months ago

inwenis commented 6 months ago
const pino = require('pino')
const logger = pino({
  transport: {
    target: 'pino-pretty',
    options: {
      colorize: true,
      sync: true
    }
  }
}, pino.destination({sync:true}))

console.log('yyy');
logger.info('hi');
console.log('xxx');

Expected:

yyy
[15:02:27.684] INFO (8368): hi
xxx

Actual:

yyy
xxx
[15:02:27.684] INFO (8368): hi

It works fine without pretty-print:

const pino = require('pino')
const logger = pino({}, pino.destination({sync:true}))

console.log('yyy');
logger.info('hi');
console.log('xxx');

Output:

yyy
{"level":30,"time":1712668061532,"pid":6376,"hostname":"LAPTOP-GOU7DNK8","msg":"hi"}
xxx
package.json

{
  "name": "test11",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "pino": "^8.20.0",
    "pino-pretty": "^11.0.0"
  }
}
mcollina commented 6 months ago

A few notes:

  1. you are using both a transport and a destination. You can't have a both (we should probably throw)
  2. the transport logic is inherently asynchronous, as it ran off thread. If you want synchronous pretty printing, you should just use it as a stream.

Adding some docs on all the above would be good, as it seems it's a source of confusion.

inwenis commented 6 months ago

thank you for the answer, that explains it

psi-4ward commented 1 week ago

the transport logic is inherently asynchronous, as it ran off thread. If you want synchronous pretty printing, you should just use it as a stream.

Can one give a short example?

jsumners commented 1 week ago

https://getpino.io/#/docs/api?id=pinooptions-destination-gt-logger https://getpino.io/#/docs/api?id=destination-number-string-object-destinationstream-sonicboomopts-writablestream