Skip to content

Commit

Permalink
Log to error file on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
vidfamn committed Jul 20, 2021
1 parent 8f9fd35 commit ae40570
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# github.com/vidfamn/OGSGameNotifier
#

VERSION="v0.1-$(shell git rev-parse --short HEAD)"
VERSION="v0.1.1-$(shell git rev-parse --short HEAD)"
BUILD_TIME=$(shell date +"%Y%m%d.%H%M%S")

build: clean
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ On success, binaries will be found in the `bin`-directory.
* https://github.com/getlantern/systray
* https://github.com/hashicorp/go-memdb
* https://github.com/ambelovsky/gosf-socketio
* https://github.com/cenkalti/backoff/v4

# Helpful resources

Expand Down
21 changes: 12 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
Expand Down Expand Up @@ -54,17 +55,19 @@ func main() {
logLevel := flag.String("log-level", "info", "log level: debug, error, warn, info")
flag.Parse()

// Checks if TTY is detected, if not logs will be written to LogFile
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
logrus.SetFormatter(&logrus.TextFormatter{})
} else {
logrus.SetFormatter(&logrus.JSONFormatter{})
f, err := os.Create(LogFile)
logrus.SetFormatter(&logrus.TextFormatter{})

// Log to file on windows
if runtime.GOOS == "windows" {
f, err := os.OpenFile(LogFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
logrus.Panic(err)
logrus.WithError(err).Error("could not create logfile")
} else {
defer f.Close()

logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetOutput(f)
}
defer f.Close()
logrus.SetOutput(f)
}

switch *logLevel {
Expand Down

0 comments on commit ae40570

Please sign in to comment.