NestJS:Pino Logger 无法正确处理 GraphQL 请求

mh3*_*377 6 javascript express graphql nestjs pino

我将 NestJS v9 与 Express 适配器和@nestjs/graphql库一起使用,并且在从 graphql 请求中提取标头并使用 pino 日志库将其附加到日志消息时遇到问题。

下面是我的 LoggerModule

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { LoggerModule as PinoLoggerModule } from 'nestjs-pino';

@Module({
  imports: [
    PinoLoggerModule.forRootAsync({
      imports: [ConfigModule.forRoot({ isGlobal: true })],
      useFactory: async (configService: ConfigService) => ({
        isGlobal: true,
        pinoHttp: {
          level: process.env.LOG_LEVEL || 'info',
          redact: configService.get<string[]>('logger.redact.fields'),
          transport: {
            target: 'pino-pretty',
            options: {
              colorize: false,
              singleLine: true,
              levelFirst: false,
              translateTime: "yyyy-mm-dd'T'HH:MM:ss.l'Z'",
              messageFormat: '{req.headers.x-correlation-id} [{context}] {msg}',
              ignore: 'pid,hostname,context,req,res.headers',
              errorLikeObjectKeys: ['err', 'error'],
            },
          },
        },
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [],
  providers: [],
})
export class LoggerModule {}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,消息格式配置为:

 messageFormat: '{req.headers.x-correlation-id} [{context}] {msg}',
Run Code Online (Sandbox Code Playgroud)

但是,日志消息不只显示req.headers.x-correlation-id上下文和消息。

我已经使用 REST 请求测试了配置,并且 id 按预期出现在日志中,因此这仅是 GraphQL 请求的问题。

有谁知道我该如何解决这个问题?

这是包含我的代码的示例 github 存储库的链接

https://github.com/mh377/nestjs-graphql-sample

我向以下库提出了问题

Nestjs-皮诺

https://github.com/iamolegga/nestjs-pino/issues/1342

pino-http

https://github.com/pinojs/pino-http/issues/273

皮诺漂亮

https://github.com/pinojs/pino-pretty/issues/402