node.js - 漂亮没有在 fastify Pretty print 中定义

标签 node.js fastify

我正在尝试在 fastify 记录器选项上使用 prettyprint: true ,如下所示:

const build = require("./app");

const startServer = async () => {
  try {
    const app = await build({
      logger: {
        prettyPrint: true
      },
    });

    const PORT = process.env.PORT;
    await app.listen(PORT);
  } catch (err) {
    console.log(err);
    process.exit(1);
  }
};

startServer().catch();

此代码来自 here官方文档here .

pino-pretty 软件包已安装

当我尝试运行服务器时出现此错误:

const formatted = pretty(typeof redact === 'function' ? redact(obj) : obj)
                        ^

TypeError: pretty is not a function

检查了这行代码(/node_modules/pino/lib/tools.js 282),pretty类型是object,但在代码中它被用作函数

const formatted = pretty(typeof redact === 'function' ? redact(obj) : obj)

有什么想法吗?

最佳答案

找到解决办法

  1. 安装 pinopino-pretty 软件包 (npm i pino pino-pretty)

  2. 通过要求 pino 并将 pretty-print: true 添加到选项来初始化记录器

const build = require("./app");

const startServer = async () => {
  try {
    const app = await build({
      logger: require("pino")({ prettyPrint: true }), // here
    });

    await app.listen(app.config.PORT);
  } catch (err) {
    console.log(err);
    process.exit(1);
  }
};

startServer().catch();

关于node.js - 漂亮没有在 fastify Pretty print 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69133907/

相关文章:

nestjs - 如何使用fastify在nestjs中设置响应头

node.js - NestJS 在构建应用程序时快速修复 swagger 错误

node.js - 推送失败! [远程拒绝] master -> master(预接收 Hook 被拒绝)

javascript - Node HTTP 和 WS 服务器未按预期升级连接

javascript - NodeJS Crypto加密到前端javascript解密

javascript - fastify-plugin 的具体用途是什么

node.js - fastify中如何组织路由?

javascript - node/express 强制浏览器使用自定义名称下载文件

arrays - Mongoose 查询 : Find an element inside an array

javascript - Fastify 中间件 - 访问查询和参数?