2020-06-18 16:54:48 -04:00
|
|
|
package logger
|
|
|
|
|
|
|
|
// For debugging
|
|
|
|
|
|
|
|
import (
|
2021-12-07 19:23:12 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-06-18 16:54:48 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2021-12-07 19:23:12 -05:00
|
|
|
var logger *log.Logger
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2021-12-07 19:23:12 -05:00
|
|
|
func GetLogger() (*log.Logger, error) {
|
|
|
|
if logger != nil {
|
|
|
|
return logger, nil
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
2021-12-07 19:23:12 -05:00
|
|
|
|
|
|
|
var writer io.Writer
|
|
|
|
var err error
|
|
|
|
|
|
|
|
debugModeEnabled := os.Getenv("AMFORA_DEBUG") == "1"
|
|
|
|
if debugModeEnabled {
|
|
|
|
writer, err = os.Create("debug.log")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Suppress all logging output if debug mode is disabled
|
|
|
|
writer = ioutil.Discard
|
|
|
|
}
|
|
|
|
|
|
|
|
logger = log.New(writer, "", log.LstdFlags)
|
|
|
|
|
|
|
|
if !debugModeEnabled {
|
|
|
|
// Clear all flags to skip log output formatting step to increase
|
|
|
|
// performance somewhat if we're not logging anything
|
|
|
|
logger.SetFlags(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Println("Started logger")
|
|
|
|
|
|
|
|
return logger, nil
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|