How to disable a log.Logger

Matt Joiner picture Matt Joiner · May 13, 2012 · Viewed 22.7k times · Source

I have some heavily instrumented code that makes use of the log package. Now it's come time to turn off the logging, and I can't determine how to turn off the standard logger.

Have I missed something? Should I be checking a flag before making log calls, or commenting them out in production?

Answer

Jeff Wendling picture Jeff Wendling · May 21, 2012

No reason to create your own type for a common io.Writer when one exists in the io/ioutil package.

import (
    "log"
    "io/ioutil"
)

func init() {
    log.SetOutput(ioutil.Discard)
}