This avoids race-conditions in unit tests where the initialisation is on a different goroutine to the logging.
47 lines
896 B
Go
47 lines
896 B
Go
package common
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
const (
|
|
standardLogFlags = log.Ldate | log.Ltime | log.Lmicroseconds
|
|
)
|
|
|
|
// Largely taken from
|
|
// http://www.goinggo.net/2013/11/using-log-package-in-go.html
|
|
|
|
var (
|
|
Debug *log.Logger
|
|
Info *log.Logger
|
|
Warning *log.Logger
|
|
Error *log.Logger
|
|
debugF bool
|
|
)
|
|
|
|
func InitLogging(debugHandle io.Writer,
|
|
infoHandle io.Writer,
|
|
warningHandle io.Writer,
|
|
errorHandle io.Writer) {
|
|
|
|
Debug = log.New(debugHandle, "DEBUG: ", standardLogFlags)
|
|
Info = log.New(infoHandle, "INFO: ", standardLogFlags)
|
|
Warning = log.New(warningHandle, "WARNING: ", standardLogFlags)
|
|
Error = log.New(errorHandle, "ERROR: ", standardLogFlags)
|
|
}
|
|
|
|
func InitDefaultLogging(debug bool) {
|
|
if debug == debugF {
|
|
return
|
|
}
|
|
debugF = debug
|
|
debugOut := ioutil.Discard
|
|
if debug {
|
|
debugOut = os.Stderr
|
|
}
|
|
InitLogging(debugOut, os.Stdout, os.Stdout, os.Stderr)
|
|
}
|