Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync doesn't work #504

Open
inwenis opened this issue Apr 9, 2024 · 6 comments
Open

sync doesn't work #504

inwenis opened this issue Apr 9, 2024 · 6 comments
Labels

Comments

@inwenis
Copy link

inwenis commented Apr 9, 2024

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
Copy link
Member

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
Copy link
Author

inwenis commented Apr 12, 2024

thank you for the answer, that explains it

@psi-4ward
Copy link

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?

@hanoii
Copy link

hanoii commented Nov 4, 2024

Can one give a short example?

import pretty from 'pino-pretty'

// Initialize logger
const logger = pino(pretty({sync: true}))

@psi-4ward
Copy link

import pretty from 'pino-pretty'

// Initialize logger
const logger = pino(pretty({sync: true}))

Yes but the request was how to use it with other transports.

I've solved it by constructing my streams manually:

const pinoOptions: LoggerOptions = {
  // Set the global log level to the lowest level
  // We adjust the level per transport
  level: 'trace',
  hooks: {},
  serializers: {
    // Handle error properties as Error and serialize them correctly
    err: pino.stdSerializers.err,
    error: pino.stdSerializers.err,
    validationErrors: pino.stdSerializers.err,
  },
};

const streams: StreamEntry[] = [];

streams.push({
  level: "info",
  stream: PinoPretty({
    sync: true,
    colorize: true,
  })
});

streams.push({
  level: level,
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  stream: pino.transport({
    target: './app.log'
  })
});

streams.push({
  level: "debug",
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  stream: pino.transport({
    target: 'pino-opentelemetry-transport',
  })
});

export const baseLogger = pino(pinoOptions, pino.multistream(streams));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants