Skip to content

Commit

Permalink
Merge branch 'main' into fix/djj/etcd_registry_lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Disdjj committed Feb 3, 2024
2 parents a125ee9 + c7fa51d commit 96a7318
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
36 changes: 28 additions & 8 deletions contrib/log/zap/zap.go
Expand Up @@ -11,36 +11,56 @@ 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
}

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
}
Expand Down
2 changes: 2 additions & 0 deletions contrib/log/zap/zap_test.go
Expand Up @@ -45,13 +45,15 @@ 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",
"{\"level\":\"info\",\"msg\":\"\",\"log\":\"info\"}\n",
"{\"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] {
Expand Down
5 changes: 5 additions & 0 deletions encoding/form/form_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"reflect"
"testing"
"time"

"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions encoding/form/proto_encode_test.go
Expand Up @@ -2,6 +2,7 @@ package form

import (
"testing"
"time"

"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
Expand All @@ -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",
Expand Down

0 comments on commit 96a7318

Please sign in to comment.