Skip to content

Commit

Permalink
Allow logfile location to be changed with LOG_FILE env var fixes sysw…
Browse files Browse the repository at this point in the history
  • Loading branch information
joshrendek committed Nov 4, 2024
1 parent cecaa9c commit 879b140
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 879b140

Please sign in to comment.