When to use log over fmt for debugging and printing error?
Select between log
and fmt
using these facts:
- The
log
functions print to stderr by default and can directed to an arbitrary writer. Thefmt.Printf
function prints to stdout. - The
log
functions can print timestamp, source code location and other info. - The
log
functions andfmt.Printf
are both thread safe, but concurrent writes byfmt.Printf
above an OS dependent size can be interleaved.
The answer to the three sub questions are "it depends".
I would like to add one more point:
Log is thread safe where as fmt is not.
A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.
Link