如何在实时输出的同时将命令的标准输出和标准错误重定向到控制台和日志文件? [英] How can I redirect the stdout and stderr of a command to both the console and a log file while outputting in real time?

查看:27
本文介绍了如何在实时输出的同时将命令的标准输出和标准错误重定向到控制台和日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码完全符合我的要求,只是它只打印到控制台.

The following bit of code does exactly what I want, except it only prints to the console.

cmd := exec.Command("php", "randomcommand.php")

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
    log.Fatal(err)
}

随机命令.php:

// randomcommand.php simply alternates output between stdout and stderr 20 times

$stdout = fopen('php://stdout', 'w+');
$stderr = fopen('php://stderr', 'w+');
for ($i = 1;$i <= 20; $i++) {
    fwrite($stdout, "stdout $i
");
    fwrite($stderr, "stderr $i
");
}

输出:

stdout 1
stderr 1
stdout 2
stderr 2
stdout 3
stderr 3
stdout 4
stderr 4
stdout 5
stderr 5
stdout 6
stderr 6
stdout 7
stderr 7
stdout 8
stderr 8
stdout 9
stderr 9
stdout 10
stderr 10
stdout 11
stderr 11
stdout 12
stderr 12
stdout 13
stderr 13
stdout 14
stderr 14
stdout 15
stderr 15
stdout 16
stderr 16
stdout 17
stderr 17
stdout 18
stderr 18
stdout 19
stderr 19
stdout 20
stderr 20

我想要实现的是:

  1. 将命令的stdout和stderr实时打印到控制台;
  2. 将命令的 stdout 和 stderr 记录到文件中,与控制台中显示的完全相同;
  3. 遵守写入 stdout 和 stderr 的确切顺序,并且;
  4. 不修改命令本身

我尝试将命令 stdout 和 stderr 传送到扫描仪或使用 io.Copy,每个都在一个 goroutine 中,但输出的顺序没有得到维护.可能是因为 goroutine 之间的交替必须与输出交替同时发生,这几乎是不可能的.

I've tried piping the command stdout and stderr into scanners or using io.Copy, each in a goroutine, but order of the output was not maintained. Probably because alternating between goroutines would have to occur at the same time as output alternates, which is just about impossible.

我还尝试了使用select",如 这里.但是输出顺序没有保持.

I also tried the using "select" as in the examples from here. But order of output was not maintained.

Logstreamer 也有同样的问题.

我必须尝试的另一个想法是看看我是否可以以某种方式从控制台缓冲区中读取,但这感觉不对.

The other idea I have to try is to see if I can somehow just read from the console buffer, but this just doesn't feel right.

有谁知道这是否可能并且值得追求?

Does anyone know if this is even possible and worth pursuing?

推荐答案

正如我在评论部分所说,这可以使用 MultiWriter

As I said in the comment section, this can be achieved using MultiWriter

package main

import (
    "io"
    "log"
    "os"
    "os/exec"
)

func main() {
    // Logging capability
    f, err := os.OpenFile("log.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    if err != nil {
        log.Fatalf("Error opening file: %v", err)
    }
    defer f.Close()
    mwriter := io.MultiWriter(f, os.Stdout)
    cmd := exec.Command("ls")
    cmd.Stderr = mwriter
    cmd.Stdout = mwriter
    err = cmd.Run() //blocks until sub process is complete
    if err != nil {
        panic(err)
    }
}

当你声明你的命令时,在你运行它之前,只需指定 Stdout 和 Stderr 使用上面定义的 MultiWriter.这个 MultiWriter 实例包含一个日志文件和标准输出.

When you declare your command, and before you run it, just specify that Stdout and Stderr are using the MultiWriter defined above. This MultiWriter instance contains both a log file and the standard output.

这篇关于如何在实时输出的同时将命令的标准输出和标准错误重定向到控制台和日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆