Skip to content

Commit

Permalink
fix for windows paths uber-go/zap#621
Browse files Browse the repository at this point in the history
  • Loading branch information
awillis committed Feb 9, 2019
1 parent 02e3fe1 commit d4ab59e
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions core/logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package core

import (
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -31,17 +33,25 @@ func LogConfig(name string, id string) *zap.Config {
}

logfile := new(strings.Builder)

if runtime.GOOS == "windows" {
logfile.WriteString("file://localhost/")
logfile.WriteString("windows:///")

// fix for windows paths: https://github.com/uber-go/zap/issues/621
err := zap.RegisterSink("windows", func(i *url.URL) (sink zap.Sink, e error) {
return os.OpenFile(i.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
})

if err != nil {
panic(err)
}
}

logfile.WriteString(LOGDIR)
logfile.WriteRune(os.PathSeparator)
logfile.WriteString(basename)
logfile.WriteString(".log")

//fmt.Println(filepath.FromSlash(filepath.Clean(logfile.String())))

return &zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
DisableStacktrace: true,
Expand All @@ -62,7 +72,7 @@ func LogConfig(name string, id string) *zap.Config {
}),
EncodeDuration: zapcore.SecondsDurationEncoder,
},
OutputPaths: []string{logfile.String()},
OutputPaths: []string{filepath.Clean(logfile.String())},
InitialFields: fields,
}
}

0 comments on commit d4ab59e

Please sign in to comment.