【问题标题】:How to Ignore a specific route logging using Fastify in NestJs?如何在 NestJs 中使用 Fastify 忽略特定的路由日志记录?
【发布时间】:2020-02-05 17:18:50
【问题描述】:

我想在我的 NestJs 应用程序中使用 Fastify 忽略或更改路由的 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
        }),
    )

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

请在这方面提供帮助。

【问题讨论】:

  • 你找到解决办法了吗?
  • @BinaryShrub 是的。我发布了对我们有用的解决方案。

标签: node.js logging nestjs fastify


【解决方案1】:

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

import fastify from 'fastify'


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2015-02-25
    • 2018-07-24
    • 2019-06-11
    相关资源
    最近更新 更多