From f8009bdd6b034d2050d23b8cf13595604778e460 Mon Sep 17 00:00:00 2001 From: Jeff Ortel Date: Fri, 21 Jun 2024 08:49:33 -0700 Subject: [PATCH] Show fields when logging object. Signed-off-by: Jeff Ortel --- logr/log_test.go | 26 ++++++++++++++++++++++++++ logr/logger.go | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 logr/log_test.go diff --git a/logr/log_test.go b/logr/log_test.go new file mode 100644 index 0000000..9ffe0a5 --- /dev/null +++ b/logr/log_test.go @@ -0,0 +1,26 @@ +package logr + +import ( + "testing" + + liberr "github.com/jortel/go-utils/error" +) + +func TestList(t *testing.T) { + log := WithName("Test") + type Persion struct { + Name string + Age int + } + p := Persion{ + Name: "Elmer", + Age: 60, + } + + err := liberr.New("Test") + + log.Info("Test") + log.Info("Test", "person", p) + log.Error(err, "Test") + log.Error(err, "Test", "person", p) +} diff --git a/logr/logger.go b/logr/logger.go index e91281e..029c247 100644 --- a/logr/logger.go +++ b/logr/logger.go @@ -148,7 +148,8 @@ func fields(kvpair []any) log.Fields { for i := range kvpair { if i%2 != 0 { key := fmt.Sprintf("%v", kvpair[i-1]) - fields[key] = kvpair[i] + v := fmt.Sprintf("%+v", kvpair[i]) + fields[key] = v } } return fields