Skip to content

Commit 74cf37a

Browse files
GRbitrstlipoca9
authored
Add EmptyFields method to remove all the fileds from logger (#575)
Co-authored-by: Olivier Poitrey <[email protected]> Co-authored-by: tlipoca9 <[email protected]>
1 parent e5edd4b commit 74cf37a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

context.go

+6
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@ func (c Context) Any(key string, i interface{}) Context {
409409
return c.Interface(key, i)
410410
}
411411

412+
// Reset removes all the context fields.
413+
func (c Context) Reset() Context {
414+
c.l.context = enc.AppendBeginMarker(make([]byte, 0, 500))
415+
return c
416+
}
417+
412418
type callerHook struct {
413419
callerSkipFrameCount int
414420
}

log_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ func TestWith(t *testing.T) {
145145
}
146146
}
147147

148+
func TestWithReset(t *testing.T) {
149+
out := &bytes.Buffer{}
150+
ctx := New(out).With().
151+
Str("string", "foo").
152+
Stringer("stringer", net.IP{127, 0, 0, 1}).
153+
Stringer("stringer_nil", nil).
154+
Reset().
155+
Bytes("bytes", []byte("bar")).
156+
Hex("hex", []byte{0x12, 0xef}).
157+
Uint64("uint64", 10).
158+
Float64("float64", 12.30303).
159+
Ctx(context.Background())
160+
log := ctx.Logger()
161+
log.Log().Msg("")
162+
if got, want := decodeIfBinaryToString(out.Bytes()), `{"bytes":"bar","hex":"12ef","uint64":10,"float64":12.30303}`+"\n"; got != want {
163+
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
164+
}
165+
}
166+
148167
func TestFieldsMap(t *testing.T) {
149168
out := &bytes.Buffer{}
150169
log := New(out)

0 commit comments

Comments
 (0)