pino 记录器作为 fastify 插件
我已经为 fastify 记录器创建了自己的选项和流:
const logger = pino(
{
level: 'info',
...ecsFormat,
},
pinoMultiStream.multistream([
{ stream: streamToElastic },
{
stream: pretty({
colorize: true,
sync: true,
ignore: 'pid',
}),
},
]),)
const fastify = Fastify({logger})
现在我想将此选项提取为 fastify 插件,我该如何执行此功能?如果这是不可能的,我该怎么做才能提取此代码?
I have been created my own options and stream for fastify logger:
const logger = pino(
{
level: 'info',
...ecsFormat,
},
pinoMultiStream.multistream([
{ stream: streamToElastic },
{
stream: pretty({
colorize: true,
sync: true,
ignore: 'pid',
}),
},
]),)
const fastify = Fastify({logger})
now I want to extract this options as fastify plugin, how can I do this functionality? If that’s impossible what can I do to extract this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法将代码封装到 Fastify 插件中,因为当时 Fastify 的记录器已经创建。
在这种情况下,您需要定义自己的逻辑来构建 fastify 服务器的配置,例如装饰器模式。
您将获得的用户体验将类似于:
You can't encapsulate your code into a Fastify plugin because Fastify's logger has been already created at that time.
In this case, you need to define your own logic to build the fastify server's configuration such as a
decorator
pattern.The user experience you will get would be something like: