如何在 NestJs 中使用 Fastify 忽略特定路由日志记录?

Anw*_*lik 5 logging node.js nestjs fastify

我想使用 Fastify 忽略或更改 NestJs 应用程序中路由的 logLevel。

这就是我在 Fastify 应用程序中通常的做法。这里我将/health路由更改logLevel为,error以便仅在运行状况出现错误时才会记录。

server.get('/health', { logLevel: 'error' }, async (request, reply) => {
    if (mongoose.connection.readyState === 1) {
        reply.code(200).send()
    } else {
        reply.code(500).send()
    }
})
Run Code Online (Sandbox Code Playgroud)

但这是我在 NestJs 中的健康控制器

@Get('health')
getHealth(): string {
  return this.appService.getHealth()
}
Run Code Online (Sandbox Code Playgroud)

和 main.ts 文件。

const app = await NestFactory.create<NestFastifyApplication>(
        AppModule,
        new FastifyAdapter({
            logger: true
        }),
    )
Run Code Online (Sandbox Code Playgroud)

我不想只记录健康路线而不记录路线。

请在这方面提供帮助。

Anw*_*lik 5

使用 Fastify 忽略/静默 NestJS 中的特定路由解决方法。我们可以使用 Fastify hookonRoute并更改该路由的日志级别。例如忽略健康路线。

import fastify from 'fastify'


const fastifyInstance = fastify()
fastifyInstance.addHook('onRoute', opts => {
        if (opts.path === '/health') {
            opts.logLevel = 'silent'
        }
    })
Run Code Online (Sandbox Code Playgroud)