I would like to redirect the output of a process to log in a timely manner. I can do it if I wait for the process to finish like this:
cmd := exec.Command("yes", "Go is awesome") // Prints "Go is awesome", forever
out, err := cmd.CombinedOutput()
log.Printf("%s", out)
However, if the process takes a long time or doesn't finish this is less useful. I know I can write to stdout in real time like this:
cmd := exec.Command("yes")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
This doesn't really help me though, because I am writing a service that isn't writing to a terminal. I'm looking for something that will let me do something like:
cmd := exec.Command("yes")
cmd.Stdout = log.Stdout
cmd.Stderr = log.Stdout
cmd.Run()
log doesn't give direct access to its writer so this is not possible. Surely I'm not the only with this problem, how is this typically done?