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)
我不想只记录健康路线而不记录路线。
请在这方面提供帮助。
使用 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)
归档时间: |
|
查看次数: |
3247 次 |
最近记录: |