diff --git a/deprecated_levels/levels.go b/deprecated_levels/levels.go deleted file mode 100644 index a034212..0000000 --- a/deprecated_levels/levels.go +++ /dev/null @@ -1,127 +0,0 @@ -package levels - -import "github.com/go-kit/kit/log" - -// Levels provides a leveled logging wrapper around a logger. It has five -// levels: debug, info, warning (warn), error, and critical (crit). If you -// want a different set of levels, you can create your own levels type very -// easily, and you can elide the configuration. -type Levels struct { - logger log.Logger - levelKey string - - // We have a choice between storing level values in string fields or - // making a separate context for each level. When using string fields the - // Log method must combine the base context, the level data, and the - // logged keyvals; but the With method only requires updating one context. - // If we instead keep a separate context for each level the Log method - // must only append the new keyvals; but the With method would have to - // update all five contexts. - - // Roughly speaking, storing multiple contexts breaks even if the ratio of - // Log/With calls is more than the number of levels. We have chosen to - // make the With method cheap and the Log method a bit more costly because - // we do not expect most applications to Log more than five times for each - // call to With. - - debugValue string - infoValue string - warnValue string - errorValue string - critValue string -} - -// New creates a new leveled logger, wrapping the passed logger. -func New(logger log.Logger, options ...Option) Levels { - l := Levels{ - logger: logger, - levelKey: "level", - - debugValue: "debug", - infoValue: "info", - warnValue: "warn", - errorValue: "error", - critValue: "crit", - } - for _, option := range options { - option(&l) - } - return l -} - -// With returns a new leveled logger that includes keyvals in all log events. -func (l Levels) With(keyvals ...interface{}) Levels { - return Levels{ - logger: log.With(l.logger, keyvals...), - levelKey: l.levelKey, - debugValue: l.debugValue, - infoValue: l.infoValue, - warnValue: l.warnValue, - errorValue: l.errorValue, - critValue: l.critValue, - } -} - -// Debug returns a debug level logger. -func (l Levels) Debug() log.Logger { - return log.WithPrefix(l.logger, l.levelKey, l.debugValue) -} - -// Info returns an info level logger. -func (l Levels) Info() log.Logger { - return log.WithPrefix(l.logger, l.levelKey, l.infoValue) -} - -// Warn returns a warning level logger. -func (l Levels) Warn() log.Logger { - return log.WithPrefix(l.logger, l.levelKey, l.warnValue) -} - -// Error returns an error level logger. -func (l Levels) Error() log.Logger { - return log.WithPrefix(l.logger, l.levelKey, l.errorValue) -} - -// Crit returns a critical level logger. -func (l Levels) Crit() log.Logger { - return log.WithPrefix(l.logger, l.levelKey, l.critValue) -} - -// Option sets a parameter for leveled loggers. -type Option func(*Levels) - -// Key sets the key for the field used to indicate log level. By default, -// the key is "level". -func Key(key string) Option { - return func(l *Levels) { l.levelKey = key } -} - -// DebugValue sets the value for the field used to indicate the debug log -// level. By default, the value is "debug". -func DebugValue(value string) Option { - return func(l *Levels) { l.debugValue = value } -} - -// InfoValue sets the value for the field used to indicate the info log level. -// By default, the value is "info". -func InfoValue(value string) Option { - return func(l *Levels) { l.infoValue = value } -} - -// WarnValue sets the value for the field used to indicate the warning log -// level. By default, the value is "warn". -func WarnValue(value string) Option { - return func(l *Levels) { l.warnValue = value } -} - -// ErrorValue sets the value for the field used to indicate the error log -// level. By default, the value is "error". -func ErrorValue(value string) Option { - return func(l *Levels) { l.errorValue = value } -} - -// CritValue sets the value for the field used to indicate the critical log -// level. By default, the value is "crit". -func CritValue(value string) Option { - return func(l *Levels) { l.critValue = value } -} diff --git a/deprecated_levels/levels_test.go b/deprecated_levels/levels_test.go deleted file mode 100644 index 8d4a7f5..0000000 --- a/deprecated_levels/levels_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package levels_test - -import ( - "bytes" - "os" - "testing" - - "github.com/go-kit/kit/log" - levels "github.com/go-kit/kit/log/deprecated_levels" -) - -func TestDefaultLevels(t *testing.T) { - buf := bytes.Buffer{} - logger := levels.New(log.NewLogfmtLogger(&buf)) - - logger.Debug().Log("msg", "résumé") // of course you'd want to do this - if want, have := "level=debug msg=résumé\n", buf.String(); want != have { - t.Errorf("want %#v, have %#v", want, have) - } - - buf.Reset() - logger.Info().Log("msg", "Åhus") - if want, have := "level=info msg=Åhus\n", buf.String(); want != have { - t.Errorf("want %#v, have %#v", want, have) - } - - buf.Reset() - logger.Error().Log("msg", "© violation") - if want, have := "level=error msg=\"© violation\"\n", buf.String(); want != have { - t.Errorf("want %#v, have %#v", want, have) - } - - buf.Reset() - logger.Crit().Log("msg", " ") - if want, have := "level=crit msg=\"\\t\"\n", buf.String(); want != have { - t.Errorf("want %#v, have %#v", want, have) - } -} - -func TestModifiedLevels(t *testing.T) { - buf := bytes.Buffer{} - logger := levels.New( - log.NewJSONLogger(&buf), - levels.Key("l"), - levels.DebugValue("dbg"), - levels.InfoValue("nfo"), - levels.WarnValue("wrn"), - levels.ErrorValue("err"), - levels.CritValue("crt"), - ) - logger.With("easter_island", "176°").Debug().Log("msg", "moai") - if want, have := `{"easter_island":"176°","l":"dbg","msg":"moai"}`+"\n", buf.String(); want != have { - t.Errorf("want %#v, have %#v", want, have) - } -} - -func ExampleLevels() { - logger := levels.New(log.NewLogfmtLogger(os.Stdout)) - logger.Debug().Log("msg", "hello") - logger.With("context", "foo").Warn().Log("err", "error") - - // Output: - // level=debug msg=hello - // level=warn context=foo err=error -} diff --git a/logrus/logrus_logger.go b/logrus/logrus_logger.go deleted file mode 100644 index a5dc9ca..0000000 --- a/logrus/logrus_logger.go +++ /dev/null @@ -1,69 +0,0 @@ -// Package logrus provides an adapter to the -// go-kit log.Logger interface. -package logrus - -import ( - "errors" - "fmt" - - "github.com/go-kit/kit/log" - "github.com/sirupsen/logrus" -) - -type Logger struct { - field logrus.FieldLogger - level logrus.Level -} - -type Option func(*Logger) - -var errMissingValue = errors.New("(MISSING)") - -// NewLogger returns a Go kit log.Logger that sends log events to a logrus.Logger. -func NewLogger(logger logrus.FieldLogger, options ...Option) log.Logger { - l := &Logger{ - field: logger, - level: logrus.InfoLevel, - } - - for _, optFunc := range options { - optFunc(l) - } - - return l -} - -// WithLevel configures a logrus logger to log at level for all events. -func WithLevel(level logrus.Level) Option { - return func(c *Logger) { - c.level = level - } -} - -func (l Logger) Log(keyvals ...interface{}) error { - fields := logrus.Fields{} - for i := 0; i < len(keyvals); i += 2 { - if i+1 < len(keyvals) { - fields[fmt.Sprint(keyvals[i])] = keyvals[i+1] - } else { - fields[fmt.Sprint(keyvals[i])] = errMissingValue - } - } - - switch l.level { - case logrus.InfoLevel: - l.field.WithFields(fields).Info() - case logrus.ErrorLevel: - l.field.WithFields(fields).Error() - case logrus.DebugLevel: - l.field.WithFields(fields).Debug() - case logrus.WarnLevel: - l.field.WithFields(fields).Warn() - case logrus.TraceLevel: - l.field.WithFields(fields).Trace() - default: - l.field.WithFields(fields).Print() - } - - return nil -} diff --git a/logrus/logrus_logger_test.go b/logrus/logrus_logger_test.go deleted file mode 100644 index cc68184..0000000 --- a/logrus/logrus_logger_test.go +++ /dev/null @@ -1,119 +0,0 @@ -package logrus_test - -import ( - "bytes" - "encoding/json" - "errors" - "strings" - "testing" - - log "github.com/go-kit/kit/log/logrus" - "github.com/sirupsen/logrus" -) - -func TestLogrusLogger(t *testing.T) { - t.Parallel() - buf := &bytes.Buffer{} - logrusLogger := logrus.New() - logrusLogger.Out = buf - logrusLogger.Formatter = &logrus.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true} - logger := log.NewLogger(logrusLogger) - - if err := logger.Log("hello", "world"); err != nil { - t.Fatal(err) - } - if want, have := "hello=world\n", strings.Split(buf.String(), " ")[3]; want != have { - t.Errorf("want %#v, have %#v", want, have) - } - - buf.Reset() - if err := logger.Log("a", 1, "err", errors.New("error")); err != nil { - t.Fatal(err) - } - if want, have := "a=1 err=error", strings.TrimSpace(strings.SplitAfterN(buf.String(), " ", 4)[3]); want != have { - t.Errorf("want %#v, have %#v", want, have) - } - - buf.Reset() - if err := logger.Log("a", 1, "b"); err != nil { - t.Fatal(err) - } - if want, have := "a=1 b=\"(MISSING)\"", strings.TrimSpace(strings.SplitAfterN(buf.String(), " ", 4)[3]); want != have { - t.Errorf("want %#v, have %#v", want, have) - } - - buf.Reset() - if err := logger.Log("my_map", mymap{0: 0}); err != nil { - t.Fatal(err) - } - if want, have := "my_map=special_behavior", strings.TrimSpace(strings.Split(buf.String(), " ")[3]); want != have { - t.Errorf("want %#v, have %#v", want, have) - } -} - -type mymap map[int]int - -func (m mymap) String() string { return "special_behavior" } - -func TestWithLevel(t *testing.T) { - tests := []struct { - name string - level logrus.Level - expectedLevel logrus.Level - }{ - { - name: "Test Debug level", - level: logrus.DebugLevel, - expectedLevel: logrus.DebugLevel, - }, - { - name: "Test Error level", - level: logrus.ErrorLevel, - expectedLevel: logrus.ErrorLevel, - }, - { - name: "Test Warn level", - level: logrus.WarnLevel, - expectedLevel: logrus.WarnLevel, - }, - { - name: "Test Info level", - level: logrus.InfoLevel, - expectedLevel: logrus.InfoLevel, - }, - { - name: "Test Trace level", - level: logrus.TraceLevel, - expectedLevel: logrus.TraceLevel, - }, - { - name: "Test not existing level", - level: 999, - expectedLevel: logrus.InfoLevel, - }, - } - for _, tt := range tests { - buf := &bytes.Buffer{} - logrusLogger := logrus.New() - logrusLogger.Out = buf - logrusLogger.Level = tt.level - logrusLogger.Formatter = &logrus.JSONFormatter{} - logger := log.NewLogger(logrusLogger, log.WithLevel(tt.level)) - - t.Run(tt.name, func(t *testing.T) { - if err := logger.Log(); err != nil { - t.Fatal(err) - } - - l := map[string]interface{}{} - if err := json.Unmarshal(buf.Bytes(), &l); err != nil { - t.Fatal(err) - } - - if v, ok := l["level"].(string); !ok || v != tt.expectedLevel.String() { - t.Fatalf("Logging levels doesn't match. Expected: %s, got: %s", tt.level, v) - } - - }) - } -} diff --git a/zap/zap_sugar_logger.go b/zap/zap_sugar_logger.go deleted file mode 100644 index 97fe645..0000000 --- a/zap/zap_sugar_logger.go +++ /dev/null @@ -1,40 +0,0 @@ -package zap - -import ( - "github.com/go-kit/kit/log" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -type zapSugarLogger func(msg string, keysAndValues ...interface{}) - -func (l zapSugarLogger) Log(kv ...interface{}) error { - l("", kv...) - return nil -} - -// NewZapSugarLogger returns a Go kit log.Logger that sends -// log events to a zap.Logger. -func NewZapSugarLogger(logger *zap.Logger, level zapcore.Level) log.Logger { - sugarLogger := logger.WithOptions(zap.AddCallerSkip(2)).Sugar() - var sugar zapSugarLogger - switch level { - case zapcore.DebugLevel: - sugar = sugarLogger.Debugw - case zapcore.InfoLevel: - sugar = sugarLogger.Infow - case zapcore.WarnLevel: - sugar = sugarLogger.Warnw - case zapcore.ErrorLevel: - sugar = sugarLogger.Errorw - case zapcore.DPanicLevel: - sugar = sugarLogger.DPanicw - case zapcore.PanicLevel: - sugar = sugarLogger.Panicw - case zapcore.FatalLevel: - sugar = sugarLogger.Fatalw - default: - sugar = sugarLogger.Infow - } - return sugar -} diff --git a/zap/zap_sugar_logger_test.go b/zap/zap_sugar_logger_test.go deleted file mode 100644 index 883ce0a..0000000 --- a/zap/zap_sugar_logger_test.go +++ /dev/null @@ -1,86 +0,0 @@ -package zap_test - -import ( - "encoding/json" - kitzap "github.com/go-kit/kit/log/zap" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "strings" - "testing" -) - -func TestZapSugarLogger(t *testing.T) { - // logger config - encoderConfig := zap.NewDevelopmentEncoderConfig() - encoder := zapcore.NewJSONEncoder(encoderConfig) - levelKey := encoderConfig.LevelKey - // basic test cases - type testCase struct { - level zapcore.Level - kvs []interface{} - want map[string]string - } - testCases := []testCase{ - {level: zapcore.DebugLevel, kvs: []interface{}{"key1", "value1"}, - want: map[string]string{levelKey: "DEBUG", "key1": "value1"}}, - - {level: zapcore.InfoLevel, kvs: []interface{}{"key2", "value2"}, - want: map[string]string{levelKey: "INFO", "key2": "value2"}}, - - {level: zapcore.WarnLevel, kvs: []interface{}{"key3", "value3"}, - want: map[string]string{levelKey: "WARN", "key3": "value3"}}, - - {level: zapcore.ErrorLevel, kvs: []interface{}{"key4", "value4"}, - want: map[string]string{levelKey: "ERROR", "key4": "value4"}}, - - {level: zapcore.DPanicLevel, kvs: []interface{}{"key5", "value5"}, - want: map[string]string{levelKey: "DPANIC", "key5": "value5"}}, - - {level: zapcore.PanicLevel, kvs: []interface{}{"key6", "value6"}, - want: map[string]string{levelKey: "PANIC", "key6": "value6"}}, - } - // test - for _, testCase := range testCases { - t.Run(testCase.level.String(), func(t *testing.T) { - // make logger - writer := &tbWriter{tb: t} - logger := zap.New( - zapcore.NewCore(encoder, zapcore.AddSync(writer), zap.DebugLevel), - zap.Development()) - // check panic - shouldPanic := testCase.level >= zapcore.DPanicLevel - kitLogger := kitzap.NewZapSugarLogger(logger, testCase.level) - defer func() { - isPanic := recover() != nil - if shouldPanic != isPanic { - t.Errorf("test level %v should panic(%v), but %v", testCase.level, shouldPanic, isPanic) - } - // check log kvs - logMap := make(map[string]string) - err := json.Unmarshal([]byte(writer.sb.String()), &logMap) - if err != nil { - t.Errorf("unmarshal error: %v", err) - } else { - for k, v := range testCase.want { - vv, ok := logMap[k] - if !ok || v != vv { - t.Error("error log") - } - } - } - }() - kitLogger.Log(testCase.kvs...) - }) - } -} - -type tbWriter struct { - tb testing.TB - sb strings.Builder -} - -func (w *tbWriter) Write(b []byte) (n int, err error) { - w.tb.Logf(string(b)) - w.sb.Write(b) - return len(b), nil -}