Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pino-pretty customPrettifiers doesnt work

I want to contomize the time output in terminal with customPrettifiers but it gives me error

import pino from "pino"

const logger = pino({
    transport: {
        targets: [
            { target: "pino-pretty", options: { destination: "./logs.log", mkdir: true, colorize: false } },
            {
                target: "pino-pretty",
                options: {
                    destination: process.stdout.fd,
                    customPrettifiers: {
                        time: (timestamp) => `Time: ${timestamp}`,
                    },
                },
            },
        ],
    },
})

logger.info("hi")

gives me error :

node:internal/per_context/domexception:53
    ErrorCaptureStackTrace(this);
    ^
DOMException [DataCloneError]: (timestamp) => `Time: ${timestamp}` could not be cloned.
    at new DOMException (node:internal/per_context/domexception:53:5)
    at new Worker (node:internal/worker:266:17)
like image 341
Raghavan Vidhyasagar Avatar asked Dec 06 '25 13:12

Raghavan Vidhyasagar


1 Answers

As mentioned in the comment above, it is not possible to use customPrettifiers since a certain version of Pino.

But the desired result still can be achieved via messageFormat in a separate module.

Within messageFormat, standard log fields are accessible as properties of a log parameter.

transport.js

import pinoPretty from 'pino-pretty';

export default (opts) => pinoPretty({
  ...opts,
  messageFormat: (log, messageKey) => `Time: ${Date(log.time)}, ${log[messageKey]}`
});

index.js

import pino from 'pino'

const logger = pino({
    transport: {
        targets: [
            { target: "pino-pretty", options: { destination: "./logs.log", mkdir: true, colorize: false } },
            {
                target: "./transport",
                options: {
                    destination: process.stdout.fd,
                    // customPrettifiers: {
                    //     time: (timestamp) => `Time: ${timestamp}`,
                    // },
                },
            },
        ],
    },
})

logger.info("hi")

package.json

{
  "name": "pinopretty",
  "type": "module",
  "main": "index.js",
  "dependencies": {
    "pino": "^9.5.0",
    "pino-pretty": "^13.0.0"
  }
}
like image 159
Dmitry Demidovsky Avatar answered Dec 08 '25 04:12

Dmitry Demidovsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!