Home >Backend Development >Golang >How to Disable the Standard Logger in Go?
How to Disable the Standard Logger in Go
If you're looking to disable logging in your Go code that utilizes the log package, you may wonder about the best approach. Contrary to what you might think, setting a flag or commenting out log calls isn't necessary.
Solution Using io/ioutil
Prior to Go 1.16, you can disable logging by setting the output to ioutil.Discard, a pre-defined io.Writer that ignores all writes:
import ( "log" "io/ioutil" ) func init() { log.SetOutput(ioutil.Discard) }
Solution Using io.Discard in Go 1.16
In Go 1.16 and later, you can simply use io.Discard directly, as it has been exported from the io/ioutil package:
log.SetOutput(io.Discard)
This will effectively disable all logging output from the standard logger.
The above is the detailed content of How to Disable the Standard Logger in Go?. For more information, please follow other related articles on the PHP Chinese website!