Skip to content

Commit

Permalink
support Windows log file sink (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhenghao authored Feb 5, 2023
1 parent d4aedad commit 89fc6bc
Showing 1 changed file with 24 additions and 5 deletions.
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

0 comments on commit 89fc6bc

Please sign in to comment.