From c4f435e140e7817d095de343ee8513f2b9a8774a Mon Sep 17 00:00:00 2001 From: Matthias Rampke Date: Tue, 31 Aug 2021 12:39:34 +0200 Subject: [PATCH] Revert "replace level pkg" --- main.go | 6 +-- pkg/exporter/exporter.go | 2 +- pkg/level/level.go | 84 ---------------------------------- pkg/level/level_test.go | 97 ---------------------------------------- pkg/line/line.go | 2 +- pkg/listener/listener.go | 2 +- pkg/mapper/fsm/fsm.go | 3 +- pkg/mapper/mapper.go | 2 +- 8 files changed, 6 insertions(+), 192 deletions(-) delete mode 100644 pkg/level/level.go delete mode 100644 pkg/level/level_test.go diff --git a/main.go b/main.go index 87e8221..ae999a3 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ import ( "syscall" "github.com/go-kit/log" + "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/promlog" @@ -35,7 +36,6 @@ import ( "github.com/prometheus/statsd_exporter/pkg/address" "github.com/prometheus/statsd_exporter/pkg/event" "github.com/prometheus/statsd_exporter/pkg/exporter" - "github.com/prometheus/statsd_exporter/pkg/level" "github.com/prometheus/statsd_exporter/pkg/line" "github.com/prometheus/statsd_exporter/pkg/listener" "github.com/prometheus/statsd_exporter/pkg/mapper" @@ -303,10 +303,6 @@ func main() { kingpin.HelpFlag.Short('h') kingpin.Parse() logger := promlog.New(promlogConfig) - if err := level.SetLogLevel(promlogConfig.Level.String()); err != nil { - level.Error(logger).Log("msg", "failed to set log level", "error", err) - os.Exit(1) - } parser := line.NewParser() if *dogstatsdTagsEnabled { diff --git a/pkg/exporter/exporter.go b/pkg/exporter/exporter.go index c61ab66..6e5675f 100644 --- a/pkg/exporter/exporter.go +++ b/pkg/exporter/exporter.go @@ -18,11 +18,11 @@ import ( "time" "github.com/go-kit/log" + "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/statsd_exporter/pkg/clock" "github.com/prometheus/statsd_exporter/pkg/event" - "github.com/prometheus/statsd_exporter/pkg/level" "github.com/prometheus/statsd_exporter/pkg/mapper" "github.com/prometheus/statsd_exporter/pkg/registry" ) diff --git a/pkg/level/level.go b/pkg/level/level.go deleted file mode 100644 index a06e490..0000000 --- a/pkg/level/level.go +++ /dev/null @@ -1,84 +0,0 @@ -package level - -import ( - "fmt" - - "github.com/go-kit/log" - "github.com/go-kit/log/level" -) - -var logLevel = LevelInfo - -// A Level is a logging priority. Higher levels are more important. -type Level int - -const ( - // LevelDebug logs are typically voluminous, and are usually disabled in - // production. - LevelDebug Level = iota - // LevelInfo is the default logging priority. - LevelInfo - // LevelWarn logs are more important than Info, but don't need individual - // human review. - LevelWarn - // LevelError logs are high-priority. If an application is running smoothly, - // it shouldn't generate any error-level logs. - LevelError -) - -var emptyLogger = &EmptyLogger{} - -type EmptyLogger struct{} - -func (l *EmptyLogger) Log(keyvals ...interface{}) error { - return nil -} - -// SetLogLevel sets the log level. -func SetLogLevel(level string) error { - switch level { - case "debug": - logLevel = LevelDebug - case "info": - logLevel = LevelInfo - case "warn": - logLevel = LevelWarn - case "error": - logLevel = LevelError - default: - return fmt.Errorf("unrecognized log level %s", level) - } - return nil -} - -// Error returns a logger that includes a Key/ErrorValue pair. -func Error(logger log.Logger) log.Logger { - if logLevel <= LevelError { - return level.Error(logger) - } - return emptyLogger -} - -// Warn returns a logger that includes a Key/WarnValue pair. -func Warn(logger log.Logger) log.Logger { - if logLevel <= LevelWarn { - return level.Warn(logger) - } - return emptyLogger -} - -// Info returns a logger that includes a Key/InfoValue pair. -func Info(logger log.Logger) log.Logger { - if logLevel <= LevelInfo { - return level.Info(logger) - } - return emptyLogger -} - -// Debug returns a logger that includes a Key/DebugValue pair. -func Debug(logger log.Logger) log.Logger { - if logLevel <= LevelDebug { - return level.Debug(logger) - } - return emptyLogger -} diff --git a/pkg/level/level_test.go b/pkg/level/level_test.go deleted file mode 100644 index c32715e..0000000 --- a/pkg/level/level_test.go +++ /dev/null @@ -1,97 +0,0 @@ -package level - -import ( - "bytes" - "strings" - "testing" - - "github.com/go-kit/log" -) - -func TestSetLogLevel(t *testing.T) { - tests := []struct { - name string - level string - logLevel Level - wantErr bool - }{ - {"wrong level", "foo", LevelInfo, true}, - {"level debug", "debug", LevelDebug, false}, - {"level info", "info", LevelInfo, false}, - {"level warn", "warn", LevelWarn, false}, - {"level error", "error", LevelError, false}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := SetLogLevel(tt.level); (err != nil) != tt.wantErr { - t.Fatalf("Expected log level to be set successfully, but got %v", err) - } - if tt.logLevel != logLevel { - t.Fatalf("Expected log level %v, but got %v", tt.logLevel, logLevel) - } - }) - } -} - -func TestVariousLevels(t *testing.T) { - tests := []struct { - name string - level string - want string - }{ - { - "level debug", - "debug", - strings.Join([]string{ - "level=debug log=debug", - "level=info log=info", - "level=warn log=warn", - "level=error log=error", - }, "\n"), - }, - { - "level info", - "info", - strings.Join([]string{ - "level=info log=info", - "level=warn log=warn", - "level=error log=error", - }, "\n"), - }, - { - "level warn", - "warn", - strings.Join([]string{ - "level=warn log=warn", - "level=error log=error", - }, "\n"), - }, - { - "level error", - "error", - strings.Join([]string{ - "level=error log=error", - }, "\n"), - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var buf bytes.Buffer - logger := log.NewLogfmtLogger(&buf) - - if err := SetLogLevel(tt.level); err != nil { - t.Fatalf("Expected log level to be set successfully, but got %v", err) - } - - Debug(logger).Log("log", "debug") - Info(logger).Log("log", "info") - Warn(logger).Log("log", "warn") - Error(logger).Log("log", "error") - - got := strings.TrimSpace(buf.String()) - if tt.want != got { - t.Fatalf("Expected log output %v, but got %v", tt.want, got) - } - }) - } -} diff --git a/pkg/line/line.go b/pkg/line/line.go index cd3aac2..4b77473 100644 --- a/pkg/line/line.go +++ b/pkg/line/line.go @@ -20,10 +20,10 @@ import ( "unicode/utf8" "github.com/go-kit/log" + "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/statsd_exporter/pkg/event" - "github.com/prometheus/statsd_exporter/pkg/level" "github.com/prometheus/statsd_exporter/pkg/mapper" ) diff --git a/pkg/listener/listener.go b/pkg/listener/listener.go index b1a57f5..6d07f93 100644 --- a/pkg/listener/listener.go +++ b/pkg/listener/listener.go @@ -21,10 +21,10 @@ import ( "strings" "github.com/go-kit/log" + "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/statsd_exporter/pkg/event" - "github.com/prometheus/statsd_exporter/pkg/level" ) type Parser interface { diff --git a/pkg/mapper/fsm/fsm.go b/pkg/mapper/fsm/fsm.go index 85068f7..96a162c 100644 --- a/pkg/mapper/fsm/fsm.go +++ b/pkg/mapper/fsm/fsm.go @@ -18,8 +18,7 @@ import ( "strings" "github.com/go-kit/log" - - "github.com/prometheus/statsd_exporter/pkg/level" + "github.com/go-kit/log/level" ) type mappingState struct { diff --git a/pkg/mapper/mapper.go b/pkg/mapper/mapper.go index 69a6276..12340d9 100644 --- a/pkg/mapper/mapper.go +++ b/pkg/mapper/mapper.go @@ -21,10 +21,10 @@ import ( "time" "github.com/go-kit/log" + "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" yaml "gopkg.in/yaml.v2" - "github.com/prometheus/statsd_exporter/pkg/level" "github.com/prometheus/statsd_exporter/pkg/mapper/fsm" )