How to Mute the Standard Logger
In Go, the log package provides a convenient way to log messages. However, when you need to turn off logging for performance or debugging purposes, you may wonder how to accomplish this.
To disable the standard logger, you can set its output to ioutil.Discard. This special io.Writer discards all data written to it, effectively silencing the logger. The code snippet below demonstrates this approach:
import ( "log" "io/ioutil" ) func init() { log.SetOutput(ioutil.Discard) }
For Go versions 1.16 and later, you can directly assign io.Discard to the logger's output without the need for ioutil.Discard:
log.SetOutput(io.Discard)
By implementing this solution, you can easily turn off the standard logger and prevent any logging messages from being written to the console or log file.
The above is the detailed content of How to Disable Logging in Go?. For more information, please follow other related articles on the PHP Chinese website!