Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize paths on windows #74

Merged
merged 2 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions path_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//+build !windows

package log

import (
"path/filepath"
)

func normalizePath(p string) (string, error) {
return filepath.Abs(p)
}
35 changes: 35 additions & 0 deletions path_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//+build windows

package log

import (
"fmt"
"path/filepath"
"strings"
)

func normalizePath(p string) (string, error) {
if p == "" {
return "", fmt.Errorf("path empty")
}
p, err := filepath.Abs(p)
if err != nil {
return "", err
}
// Is this _really_ an absolute path?
if !strings.HasPrefix(p, "\\\\") {
// It's a drive: path!
// Return a UNC path.
p = "\\\\%3F\\" + p
}

// This will return file:////?/c:/foobar
//
// Why? Because:
// 1. Go will choke on file://c:/ because the "domain" includes a :.
// 2. Windows will choke on file:///c:/ because the path will be
// /c:/... which is _relative_ to the current drive.
//
// This path (a) has no "domain" and (b) starts with a slash. Yay!
return "file://" + filepath.ToSlash(p), nil
}
8 changes: 6 additions & 2 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ func SetupLogging() {
zapCfg.OutputPaths = []string{"stderr"}
// check if we log to a file
if logfp := os.Getenv(envLoggingFile); len(logfp) > 0 {
zapCfg.OutputPaths = append(zapCfg.OutputPaths, logfp)
if path, err := normalizePath(logfp); err != nil {
fmt.Fprintf(os.Stderr, "failed to resolve log path '%q', logging to stderr only: %s\n", logfp, err)
} else {
zapCfg.OutputPaths = append(zapCfg.OutputPaths, path)
}
}

// set the backend(s)
Expand All @@ -83,7 +87,7 @@ func SetupLogging() {
var err error
lvl, err = LevelFromString(logenv)
if err != nil {
fmt.Println("error setting log levels", err)
fmt.Fprintf(os.Stderr, "error setting log levels: %s\n", err)
}
}
zapCfg.Level.SetLevel(zapcore.Level(lvl))
Expand Down