Example InitLog calls

Here we pass the name of our trace file, called trace-log.txt, which will receive all of the logging output. We don't want Debug information, but we do want Info and Error output:

InitLog("trace-log.txt", ioutil.Discard, os.Stdout, os.Stderr)

This time we pass nil for the name of our trace log file, which tells our logger not to create a trace log file. We do want Debug, Info, and Error data displayed to standard out in our terminal console.

InitLog(nil, os.Stdout, os.Stdout, os.Stderr)

When we specify traceFileName, we'll need to create an io.MultiWriter interface to send the output to two places at the same time:

if len(traceFileName) > 0 {
_ = os.Remove(traceFileName)
file, err := os.OpenFile(traceFileName,
os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("Failed to create log file: %s", traceFileName)
}
debugHandler = io.MultiWriter(file, debugHandler)
infoHandler = io.MultiWriter(file, infoHandler)
errorHandler = io.MultiWriter(file, errorHandler)
}

InfoHandler = infoHandler

Debug = log.New(debugHandler, "DEBUG : ",
log.Ldate|log.Ltime|log.Lshortfile)

Info = log.New(infoHandler, "INFO : ",
log.Ltime)

Error = log.New(errorHandler, "ERROR : ",
log.Ldate|log.Ltime|log.Lshortfile)
}

We'll preface each log line with DEBUG, INFO, or ERROR to indicate its log level.