From f4d949cc42b702667330d9e76d95346d0240ace8 Mon Sep 17 00:00:00 2001 From: "Pavel @grbit Griaznov" Date: Wed, 9 Aug 2023 09:03:56 +0200 Subject: [PATCH 1/6] add EmptyFields method to remove all the fileds from logger --- .gitignore | 4 ++++ context.go | 6 ++++++ log_test.go | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/.gitignore b/.gitignore index 8ebe58b1..0bdf4e76 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,10 @@ _obj _test tmp +# IDE config files +.idea +.vscode + # Architecture specific extensions/prefixes *.[568vq] [568vq].out diff --git a/context.go b/context.go index fc62ad9c..d8c1b41a 100644 --- a/context.go +++ b/context.go @@ -384,6 +384,12 @@ func (c Context) Any(key string, i interface{}) Context { return c.Interface(key, i) } +// EmptyFields removes all the context fields. +func (c Context) EmptyFields() Context { + c.l.context = enc.AppendBeginMarker(make([]byte, 0, 500)) + return c +} + type callerHook struct { callerSkipFrameCount int } diff --git a/log_test.go b/log_test.go index 4d0d93b1..fb4bc6a2 100644 --- a/log_test.go +++ b/log_test.go @@ -145,6 +145,25 @@ func TestWith(t *testing.T) { } } +func TestWithEmptyFields(t *testing.T) { + out := &bytes.Buffer{} + ctx := New(out).With(). + Str("string", "foo"). + Stringer("stringer", net.IP{127, 0, 0, 1}). + Stringer("stringer_nil", nil). + EmptyFields(). + Bytes("bytes", []byte("bar")). + Hex("hex", []byte{0x12, 0xef}). + Uint64("uint64", 10). + Float64("float64", 12.30303). + Ctx(context.Background()) + log := ctx.Logger() + log.Log().Msg("") + if got, want := decodeIfBinaryToString(out.Bytes()), `{"bytes":"bar","hex":"12ef","uint64":10,"float64":12.30303}`+"\n"; got != want { + t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want) + } +} + func TestFieldsMap(t *testing.T) { out := &bytes.Buffer{} log := New(out) From 6be9a35cc220a4e8dc23002988f6dcbd62760c36 Mon Sep 17 00:00:00 2001 From: Pavel Griaznov Date: Mon, 4 Mar 2024 09:45:25 +0000 Subject: [PATCH 2/6] Rename context cleaning function Co-authored-by: Olivier Poitrey --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context.go b/context.go index d8c1b41a..be62eb86 100644 --- a/context.go +++ b/context.go @@ -385,7 +385,7 @@ func (c Context) Any(key string, i interface{}) Context { } // EmptyFields removes all the context fields. -func (c Context) EmptyFields() Context { +func (c Context) Reset() Context { c.l.context = enc.AppendBeginMarker(make([]byte, 0, 500)) return c } From 9d4e61e88a816774a09b2745e1029ff10990770d Mon Sep 17 00:00:00 2001 From: Pavel Griaznov Date: Mon, 4 Mar 2024 09:45:50 +0000 Subject: [PATCH 3/6] Remove unrelated folders from .girignore Co-authored-by: Olivier Poitrey --- .gitignore | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.gitignore b/.gitignore index 0bdf4e76..8ebe58b1 100644 --- a/.gitignore +++ b/.gitignore @@ -8,10 +8,6 @@ _obj _test tmp -# IDE config files -.idea -.vscode - # Architecture specific extensions/prefixes *.[568vq] [568vq].out From a217e6c5a0d65bdd56d305d05183327326e161dc Mon Sep 17 00:00:00 2001 From: Pavel Griaznov Date: Mon, 4 Mar 2024 09:46:20 +0000 Subject: [PATCH 4/6] @GRbit @rs Rename context cleaning function Co-authored-by: Olivier Poitrey --- log_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log_test.go b/log_test.go index fb4bc6a2..c8a98e84 100644 --- a/log_test.go +++ b/log_test.go @@ -145,7 +145,7 @@ func TestWith(t *testing.T) { } } -func TestWithEmptyFields(t *testing.T) { +func TestWithReset(t *testing.T) { out := &bytes.Buffer{} ctx := New(out).With(). Str("string", "foo"). From e1c8c7de46b6e9b7428c582087e43f3c0623cb29 Mon Sep 17 00:00:00 2001 From: Pavel Griaznov Date: Mon, 4 Mar 2024 09:46:41 +0000 Subject: [PATCH 5/6] Rename context cleaning function Co-authored-by: Olivier Poitrey --- log_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log_test.go b/log_test.go index c8a98e84..ff21b4aa 100644 --- a/log_test.go +++ b/log_test.go @@ -151,7 +151,7 @@ func TestWithReset(t *testing.T) { Str("string", "foo"). Stringer("stringer", net.IP{127, 0, 0, 1}). Stringer("stringer_nil", nil). - EmptyFields(). + Reset(). Bytes("bytes", []byte("bar")). Hex("hex", []byte{0x12, 0xef}). Uint64("uint64", 10). From 58c5b186a6c72e96ad947c9f00f8a29f262c246e Mon Sep 17 00:00:00 2001 From: Pavel Griaznov Date: Tue, 5 Mar 2024 15:56:39 +0000 Subject: [PATCH 6/6] Rename comments EmptyFields > Reset Co-authored-by: tlipoca9 <160737620+tlipoca9@users.noreply.github.com> --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context.go b/context.go index be62eb86..c1a6ba1c 100644 --- a/context.go +++ b/context.go @@ -384,7 +384,7 @@ func (c Context) Any(key string, i interface{}) Context { return c.Interface(key, i) } -// EmptyFields removes all the context fields. +// Reset removes all the context fields. func (c Context) Reset() Context { c.l.context = enc.AppendBeginMarker(make([]byte, 0, 500)) return c