2

In shell, I use go run file > output, but only fmt.Print are saved into output. All the log.Print still output to the screen! And after I searched on the Internet, I found I can set ouput directory for log, but how can I save both log and fmt in the same file?

Haoyuan Ge
  • 3,379
  • 3
  • 24
  • 40
  • 1
    Possible duplicate of [How can I redirect and append both stdout and stderr to a file with Bash?](http://stackoverflow.com/questions/876239/how-can-i-redirect-and-append-both-stdout-and-stderr-to-a-file-with-bash) – Ainar-G Dec 15 '15 at 16:21

1 Answers1

6

The logger writes to STDERR while fmt.PrintXXX functions write to STDOUT.

You have two choices:

1) Capture STDERR to the file.
Bash like shells use:

go run file > output 2>&1

2) Set the logger's Output to os.Stdout

Then you run like normal go run file > output and both the logger and fmt go to the same place.

log.SetOutput(os.StdOut)
David Budworth
  • 11,248
  • 1
  • 36
  • 45