fmt.Printf()
documents that it writes to the standard output:
Printf formats according to a format specifier and writes to standard output.
So there is no fmt.SetOutput()
to redirect that to your file.
But note that the standard output is a variable in the os
package:
Stdin, Stdout, and Stderr are open Files pointing to the standard input, standard output, and standard error file descriptors.
Note that the Go runtime writes to standard error for panics and crashes; closing Stderr may cause those messages to go elsewhere, perhaps to a file opened later.
var ( Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin") Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout") Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr") )
And you are allowed to set your own os.File
to os.Stdout
. Although it's not a good idea to use the same os.File
for a logger and to also set it to os.Stdout
, access to its File.Write()
method would not be synchronized between the fmt
package and the logger.
Best would be to use a log.Logger
everywhere (whose output you properly set, so log messages would properly be serialized).