dongxin9759 2017-07-13 17:35
浏览 42

使用io.Multiwriter创建跨包/子包的日志

I am new to Go. Currently, I’m writing a tool that will capture all stdout outputs, and consolidate them into a new file.

In order to do so, I am using io.Multiwriter. However, my problem lies in trying to capture all of the stdout output across packages and subpackages.

Currently, my approach is to create a new package which has an exclusive role of all logging. Whenever one of the other packages/subpackages have some output string, I will send the string to the logging package, while will then multiwrite to the log and to stdout.

Does this sound reasonable? If so, what should the general logging package API look like? Also, what is good way to pass a string for output/logging across to the logging package?

Thank you!

  • 写回答

1条回答 默认 最新

  • doujia7094 2017-07-13 19:03
    关注

    I think I would be good to use already available logger packages for your purpose. Then call the logger from all your packages.

    Your requirement (mutliwriter) is typical one. Do a library search here https://golanglibs.com/search?q=logger choose the one fits for your need.

    Or learn from these package and implement your own custom tailored version.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器