Skip to content

Commit

Permalink
Merge pull request #56 from jmillerv/39-refactor-logging
Browse files Browse the repository at this point in the history
39: implement initLogger() and write to stdout and file
  • Loading branch information
jmillerv authored Jan 19, 2023
2 parents 08e34e6 + a92a054 commit ad14162
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"io"
"os"
"time"

Expand All @@ -14,6 +15,7 @@ import (
const (
configFile = "config.yml"
config_override = "GODJ_CONFIG_OVERRIDE"
logFile = "/tmp/godj.log"
)

func main() {
Expand Down Expand Up @@ -120,8 +122,29 @@ func main() {
}
}

// init sets global variables
func init() {
content.Shuffled = false
content.PodcastPlayerOrderOldest = false
content.PodcastPlayOrderRandom = false
initLogger()
}

// initLogger creates the multiwriter, determines the log format for each destination, and sets the logfile location.
// at a later stage, it may be desirable to have different formats for standard out vs the log file. An example of how to do that can be found
// here https://github.com/sirupsen/logrus/issues/784#issuecomment-403765306
func initLogger() {
// create a new file for logs
logs, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.WithError(err).Error("unable to open log file")
}
// open the multiwriter
multiWrite := io.MultiWriter(os.Stdout, logs)
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC822,
})
log.SetOutput(multiWrite)
}

0 comments on commit ad14162

Please sign in to comment.