douhu8851 2016-09-29 14:53
浏览 109

go-将stderr重定向到文件AND到stdout

I would like to redirect stderrto a file AND to stdout.

I know how redirect stderr to file using dup2 :
err := syscall.Dup2(int(fatal_logfile.Fd()), int(os.Stderr.Fd()))

But then I don't see how can redirect this another time to stdout. Like "copying" one stream-source to two differents streams.

Just a note, this functionality is called when the app starts and used to redirect stderr when, for example, a panic occurs. Then I cannot use recover in this case (even because I am running lots of goroutines).

Anyone has an idea how to solve this?


Another working solution is to use pipes and tee when calling the binary, but is possible to have the same behavior/logic defined inside the app? I would like that it runs in this way as default.

./app_bin 2> >(tee -a fatal.log)

  • 写回答

1条回答 默认 最新

  • drqja5919276 2016-09-29 15:12
    关注
    writer := io.MultiWriter(os.Stdout, os.Stderr)
    

    Whatever it's written to writer will write to os.Stdout and os.Stderr.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置