如何在Node.js CLI应用程序中使用记录和进度栏?
上下文
node.js应用程序中的
- ,我使用: node-progress for progress bar
- winston 用于登录
CLI应用程序时,在构建文件时将显示一个进度栏。在构建操作过程中,有时需要将信息/错误记录到控制台。这打扰了进度栏:
- 进度栏后立即将信息/错误日志登录到控制台,而不是在新线上
- 登录。在日志完成后,进度条再次打印出来,导致在
插图的控制台插图 中打印多个进度条。控制台:
[===========----------------------] 11 / 33 builtwarn: something wrong here.
[=============--------------------] 13 / 33 builtwarn: something wrong here.
warn: example warning that continues here.
error: some stacktrace
[=================================] 33 / 33 built
问题
是否有办法确保进度栏不会受到干扰,并且在栏下方/下方打印到控制台的任何信息日志?因此仅显示一个进度条。
我知道有一个 intrupp
node-progress中的方法,但是我不确定如何与温斯顿一起使用。
我想这在CLI应用程序中是一个相当普遍的情况,因此也赞赏有关如何通过其他依赖/方法进行的任何建议/想法!
Context
In the Node.js application, I use:
- node-progress for progress bar
- winston for logging
The CLI application will display a progress bar when building files. During the build operation, sometimes information/errors need to be logged to the console. This disturbs the progress bar in that:
- the info/error logs to the console immediately after the progress bar and not on a newline
- the progress bar gets printed again after the logs have finished, resulting in multiple progress bars printed in the console
Illustration of the console:
[===========----------------------] 11 / 33 builtwarn: something wrong here.
[=============--------------------] 13 / 33 builtwarn: something wrong here.
warn: example warning that continues here.
error: some stacktrace
[=================================] 33 / 33 built
Question
Is there a way to ensure that the progress bar is not disturbed and any information logs to the console are printed above/below the bar? Such that only one progress bar is shown.
I understand that there's a interrupt
method in node-progress, but I am not sure how to use that with winston.
I would imagine this to be a fairly common scenario in CLI applications so any suggestions/ideas of how to do it via other dependencies/approaches are appreciated too!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己做一个进步的酒吧课。
您可以打印一个
\ r
以清除屏幕上的最后一行。然后打印您的日志。
之后,打印
\ n
,请确保新的进度栏不会清除您的日志。I make a progress bar class myself.
You can print a
\r
to clear last line on the screen.Then print you log.
After that, print a
\n
, make sure new progress bar will not clear you log.