-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add a log adapter for zap.Logger #855
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 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 | ||
| } | ||
MaruHyl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return sugar | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| 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 | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.