NodeJS 无法确定“pino-pretty”的传输目标

vqlkdk9b  于 2023-11-17  发布在  Node.js
关注(0)|答案(7)|浏览(176)

我试图使用pino库,但我得到错误
我的代码
我创建了一个logger.js文件,从node_module导入pino,并添加了pino-pretty的传输。
logger.js

import pino from "pino";
    const logger = pino({
      transport: {
        target: "pino-pretty",
        options: {
          colorize: true,
        },
      },
    });
    export default logger;

字符串
我创建了一个数据库文件,并从日志文件导入pino,并使用info函数来显示我的错误。
database.js

import mongoose from "mongoose";
import logger from "./logger";
const DB_CONNECTION_STRING =
  process.env.DB_CONNECTION_STRING ||
  "mongodb://localhost:27017/*******";
try {
    await mongoose.connect(DB_CONNECTION_STRING);
    logger.info("Connect to database");
  } catch (e) {
    logger.error(e, "Failed to connect to database. Goodbye");
    process.exit(1);
  }


x1c 0d1x的数据

gjmwrych

gjmwrych1#

如果它不能与transfer选项一起使用,您可以将其用作流:

import pino from "pino";
import pretty from "pino-pretty";

const stream = pretty({
  levelFirst: true,
  colorize: true,
  ignore: "time,hostname,pid",
});

const logger = pino(
  {
    name: "MyLogger",
    level: process.env.NODE_ENV === "development" ? "debug" : "info",
  },
  stream
);

export default logger;

字符串
更多细节请参见文档:https://github.com/pinojs/pino-pretty#usage-as-a-stream

o4tp2gmn

o4tp2gmn2#

如果你没有安装它,你必须安装pino-pretty

npm i pino-pretty

字符串

deikduxw

deikduxw3#

你的package.json中可能缺少pino-pretty,你有没有尝试将它添加为依赖项?

bqucvtff

bqucvtff4#

检查环境变量。它必须是staging或production。SENTRY_ENVIRONMENT之类的检查。我也有同样的错误。


的数据

import pino, { Logger } from 'pino';
import dotenv from "dotenv";
import { createWriteStream } from "pino-sentry";

dotenv.config();

function prodLogger():Logger {
    const stream = createWriteStream({ dsn: process.env.SENTRY_DSN, serverName: process.env.TENANT_NAME});

    return pino({}, stream);
}

function devLogger(): Logger {
    return pino({
        timestamp: true,
        transport: {
            target: 'pino-pretty',
            options: {
                colorize: true,
                destination: 2,
                sync: process.env.SENTRY_ENVIRONMENT === 'testing',
            },
        },
    });
}

function logger(): Logger {
    return ['production', 'staging',"develop"].includes(process.env.SENTRY_ENVIRONMENT ?? '')
        ? prodLogger()
        : devLogger();
}

export default logger();

字符串

ryhaxcpt

ryhaxcpt5#

我在使用next-pino时遇到了这个问题。在本地工作正常,但在部署时抛出错误:
错误:无法确定“pino-pretty”的传输目标
所以我的解决方案是在我的logger类中添加pino pretty的导入:

import { Logger, pino } from 'pino'
import 'pino-pretty'

字符串

6gpjuf90

6gpjuf906#

这对我很有效

import pino from "pino";
import path from "path";

const logsPath = path.resolve("src", "logs");

const transport = pino.transport({
  targets: [
    {
      target: "pino/file",
      options: { destination: `${logsPath}/server.log` },
      level: process.env.PINO_LOG_LEVEL || "info",
    },
    //@ts-ignore
    {
      target: "pino-pretty",
      options: {
        colorize: true,
      },
    },
  ],
});

export const logger = pino(
  {
    level: process.env.PINO_LOG_LEVEL || "info",

    timestamp: pino.stdTimeFunctions.isoTime,
  },
  transport
);

字符串

2vuwiymt

2vuwiymt7#

最好的方法似乎是通过管道将输出输出到CLI工具来使用它。
所以不要在你的项目中导入它,而是将它添加到你的npm脚本中:

"scripts": {
    (...)
    "dev": "nodemon --watch src/* --exec 'node src/index.js | pino-pretty'",
    "start": "node src/index.js"
  }

字符串

相关问题