diff --git a/logging/logging.go b/logging/logging.go index d0bed3c..920f0c9 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -18,6 +18,18 @@ func LogMsg(msg string) { fmt.Println("Error writing to syslog: ", err) return } + defer logfile.Close() + if os.Getenv("LOG_FILE") != "" { + f, err := os.OpenFile(os.Getenv("LOG_FILE"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + if err != nil { + fmt.Println("Error opening log file: ", err) + return + } + defer f.Close() + log.SetOutput(f) + } else { + log.SetOutput(logfile) + } pc, _, _, _ := runtime.Caller(1) caller := runtime.FuncForPC(pc).Name() _, file, line, _ := runtime.Caller(0) @@ -26,7 +38,8 @@ func LogMsg(msg string) { pathLine := fmt.Sprintf("[%s:%d]", shortPath[1], line) logString := fmt.Sprintf("%s{%s}:: %s", pathLine, caller, msg) if os.Getenv("DEBUG") == "true" { + log.SetOutput(os.Stdout) log.Info(logString) } - logfile.Info(logString) + log.Info(logString) }