Skip to content

Commit

Permalink
refactor: DRY up fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danxmoran committed May 24, 2021
1 parent 4f93dac commit 8a6eb33
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
24 changes: 3 additions & 21 deletions cmd/influxd/upgrade/logging_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@

package upgrade

import "go.uber.org/zap"

func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) {
config := zap.NewProductionConfig()

config.Level = zap.NewAtomicLevelAt(options.logLevel)
if verbose {
config.Level.SetLevel(zap.DebugLevel)
}

config.OutputPaths = append(config.OutputPaths, options.logPath)
config.ErrorOutputPaths = append(config.ErrorOutputPaths, options.logPath)

log, err := config.Build()
if err != nil {
return nil, err
}
if verbose {
log.Warn("--verbose is deprecated, use --log-level=debug instead")
}
return log, nil
// Zap only has problems with Windows paths, so this is a no-op on Unix systems.
func (o *logOptions) zapSafeLogPath() (string, error) {
return o.logPath, nil
}
32 changes: 8 additions & 24 deletions cmd/influxd/upgrade/logging_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,23 @@ import (
"go.uber.org/zap"
)

// Work around a bug in zap's handling of absolute paths on Windows.
// See https://github.com/uber-go/zap/issues/621

const FakeWindowsScheme = "winfile"

func init() {
// NOTE: Work around a bug in zap's handling of absolute paths on Windows.
// See https://github.com/uber-go/zap/issues/621
newWinFileSink := 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)
}
zap.RegisterSink("winfile", newWinFileSink)
zap.RegisterSink(FakeWindowsScheme, newWinFileSink)
}

func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) {
config := zap.NewProductionConfig()

config.Level = zap.NewAtomicLevelAt(options.logLevel)
if verbose {
config.Level.SetLevel(zap.DebugLevel)
}

func (o *logOptions) zapSafeLogPath() (string, error) {
logPath, err := filepath.Abs(options.logPath)
if err != nil {
return nil, err
}
logPath = "winfile:///" + logPath

config.OutputPaths = append(config.OutputPaths, logPath)
config.ErrorOutputPaths = append(config.ErrorOutputPaths, logPath)

log, err := config.Build()
if err != nil {
return nil, err
}
if verbose {
log.Warn("--verbose is deprecated, use --log-level=debug instead")
}
return log, nil
return FakeWindowsScheme + ":///" + logPath, nil
}
25 changes: 25 additions & 0 deletions cmd/influxd/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,31 @@ func (i *influxDBv2) close() error {
return nil
}

func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) {
config := zap.NewProductionConfig()

config.Level = zap.NewAtomicLevelAt(options.logLevel)
if verbose {
config.Level.SetLevel(zap.DebugLevel)
}
logPath, err := options.zapSafeLogPath()
if err != nil {
return nil, err
}

config.OutputPaths = append(config.OutputPaths, logPath)
config.ErrorOutputPaths = append(config.ErrorOutputPaths, logPath)

log, err := config.Build()
if err != nil {
return nil, err
}
if verbose {
log.Warn("--verbose is deprecated, use --log-level=debug instead")
}
return log, nil
}

func runUpgradeE(ctx context.Context, ui *input.UI, options *options, log *zap.Logger) error {
if options.source.configFile != "" && options.source.dbDir != "" {
return errors.New("only one of --v1-dir or --config-file may be specified")
Expand Down

0 comments on commit 8a6eb33

Please sign in to comment.