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

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

我想使用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()
    }
})

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

@Get('health')
getHealth(): string {
  return this.appService.getHealth()
}

和main.ts文件。

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

我不想只记录健康路线,而不是日志。

请在这方面提供帮助。

node.js logging nestjs fastify
1个回答
0
投票

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

import fastify from 'fastify'


const fastifyInstance = fastify()
fastifyInstance.addHook('onRoute', opts => {
        if (opts.path === '/health') {
            opts.logLevel = 'silent'
        }
    })
© www.soinside.com 2019 - 2024. All rights reserved.