From d9f390cdbb694a3130361995a30d1165d479069c Mon Sep 17 00:00:00 2001 From: kvii <56432636+kvii@users.noreply.github.com> Date: Fri, 2 Feb 2024 10:38:20 +0800 Subject: [PATCH 1/2] fix: test failed on other timezone.(#3181) (#3183) --- encoding/form/form_test.go | 5 +++++ encoding/form/proto_encode_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/encoding/form/form_test.go b/encoding/form/form_test.go index 27a52c4b167..484e42fa2b3 100644 --- a/encoding/form/form_test.go +++ b/encoding/form/form_test.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "reflect" "testing" + "time" "google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/fieldmaskpb" @@ -86,6 +87,10 @@ func TestFormCodecUnmarshal(t *testing.T) { } func TestProtoEncodeDecode(t *testing.T) { + loc := time.Local + time.Local = time.UTC + t.Cleanup(func() { time.Local = loc }) + in := &complex.Complex{ Id: 2233, NoOne: "2233", diff --git a/encoding/form/proto_encode_test.go b/encoding/form/proto_encode_test.go index 7d0cfa9eac0..1b4e11e3500 100644 --- a/encoding/form/proto_encode_test.go +++ b/encoding/form/proto_encode_test.go @@ -2,6 +2,7 @@ package form import ( "testing" + "time" "google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/fieldmaskpb" @@ -12,6 +13,10 @@ import ( ) func TestEncodeValues(t *testing.T) { + loc := time.Local + time.Local = time.UTC + t.Cleanup(func() { time.Local = loc }) + in := &complex.Complex{ Id: 2233, NoOne: "2233", From c7fa51df7ae78935dc1a4cbefe045d6f02a83dcc Mon Sep 17 00:00:00 2001 From: Like Date: Fri, 2 Feb 2024 10:39:11 +0800 Subject: [PATCH 2/2] fix: zap log print out multiple `msg` (#3171) * fix: zap log print out multiple `msg` * fix: zap log print out multiple `msg` * feat: add message key option --- contrib/log/zap/zap.go | 36 ++++++++++++++++++++++++++++-------- contrib/log/zap/zap_test.go | 2 ++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/contrib/log/zap/zap.go b/contrib/log/zap/zap.go index 1ad8caed623..acf453fe010 100644 --- a/contrib/log/zap/zap.go +++ b/contrib/log/zap/zap.go @@ -11,15 +11,31 @@ import ( var _ log.Logger = (*Logger)(nil) type Logger struct { - log *zap.Logger + log *zap.Logger + msgKey string +} + +type Option func(*Logger) + +// WithMessageKey with message key. +func WithMessageKey(key string) Option { + return func(l *Logger) { + l.msgKey = key + } } func NewLogger(zlog *zap.Logger) *Logger { - return &Logger{zlog} + return &Logger{ + log: zlog, + msgKey: log.DefaultMessageKey, + } } func (l *Logger) Log(level log.Level, keyvals ...interface{}) error { - keylen := len(keyvals) + var ( + msg = "" + keylen = len(keyvals) + ) if keylen == 0 || keylen%2 != 0 { l.log.Warn(fmt.Sprint("Keyvalues must appear in pairs: ", keyvals)) return nil @@ -27,20 +43,24 @@ func (l *Logger) Log(level log.Level, keyvals ...interface{}) error { data := make([]zap.Field, 0, (keylen/2)+1) for i := 0; i < keylen; i += 2 { + if keyvals[i].(string) == l.msgKey { + msg, _ = keyvals[i+1].(string) + continue + } data = append(data, zap.Any(fmt.Sprint(keyvals[i]), keyvals[i+1])) } switch level { case log.LevelDebug: - l.log.Debug("", data...) + l.log.Debug(msg, data...) case log.LevelInfo: - l.log.Info("", data...) + l.log.Info(msg, data...) case log.LevelWarn: - l.log.Warn("", data...) + l.log.Warn(msg, data...) case log.LevelError: - l.log.Error("", data...) + l.log.Error(msg, data...) case log.LevelFatal: - l.log.Fatal("", data...) + l.log.Fatal(msg, data...) } return nil } diff --git a/contrib/log/zap/zap_test.go b/contrib/log/zap/zap_test.go index 393af79e993..c99573007e5 100644 --- a/contrib/log/zap/zap_test.go +++ b/contrib/log/zap/zap_test.go @@ -45,6 +45,7 @@ func TestLogger(t *testing.T) { zlog.Warnw("log", "warn") zlog.Errorw("log", "error") zlog.Errorw("log", "error", "except warn") + zlog.Info("hello world") except := []string{ "{\"level\":\"debug\",\"msg\":\"\",\"log\":\"debug\"}\n", @@ -52,6 +53,7 @@ func TestLogger(t *testing.T) { "{\"level\":\"warn\",\"msg\":\"\",\"log\":\"warn\"}\n", "{\"level\":\"error\",\"msg\":\"\",\"log\":\"error\"}\n", "{\"level\":\"warn\",\"msg\":\"Keyvalues must appear in pairs: [log error except warn]\"}\n", + "{\"level\":\"info\",\"msg\":\"hello world\"}\n", // not {"level":"info","msg":"","msg":"hello world"} } for i, s := range except { if s != syncer.output[i] {