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

support Windows log file sink #621

Merged
merged 2 commits into from
Feb 5, 2023
Merged
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
29 changes: 24 additions & 5 deletions base/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,33 @@
package log

import (
"github.com/emicklei/go-restful/v3"
"github.com/go-sql-driver/mysql"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/emicklei/go-restful/v3"
"github.com/go-sql-driver/mysql"
"github.com/samber/lo"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
)

var logger *zap.Logger

func init() {
// setup default logger
SetProductionLogger()
// Windows file sink support: https://github.com/uber-go/zap/issues/621
if runtime.GOOS == "windows" {
if err := zap.RegisterSink("windows", func(u *url.URL) (zap.Sink, error) {
// Remove leading slash left by url.Parse()
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
}); err != nil {
logger.Fatal("failed to register Windows file sink", zap.Error(err))
}
}
}

// Logger get current logger
Expand All @@ -49,7 +62,13 @@ func SetProductionLogger(outputPaths ...string) {
}
}
cfg := zap.NewProductionConfig()
cfg.OutputPaths = append(cfg.OutputPaths, outputPaths...)
if runtime.GOOS == "windows" {
cfg.OutputPaths = append(cfg.OutputPaths, lo.Map(outputPaths, func(path string, _ int) string {
return "windows:///" + path
})...)
} else {
cfg.OutputPaths = append(cfg.OutputPaths, outputPaths...)
}
var err error
logger, err = cfg.Build()
if err != nil {
Expand Down