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 基于52单片机的酒精浓度检测系统加继电器和sim800
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等