Pino-pretty 自定义美化器不起作用

问题描述 投票:0回答:1

我想用

customPrettifiers
来修改终端中的时间输出,但它给了我错误

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")

给我错误:

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)
javascript node.js logging pino
1个回答
0
投票

正如上面评论中提到的,自从 Pino 某个版本以来,无法使用

customPrettifiers

但是想要的结果仍然可以通过单独的模块中的

messageFormat
来实现。

messageFormat
内,标准日志字段可作为
log
参数的属性进行访问。

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"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.