Skip to content

Commit f4d949c

Browse files
committed
add EmptyFields method to remove all the fileds from logger
1 parent b81cc57 commit f4d949c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ _obj
88
_test
99
tmp
1010

11+
# IDE config files
12+
.idea
13+
.vscode
14+
1115
# Architecture specific extensions/prefixes
1216
*.[568vq]
1317
[568vq].out

context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ func (c Context) Any(key string, i interface{}) Context {
384384
return c.Interface(key, i)
385385
}
386386

387+
// EmptyFields removes all the context fields.
388+
func (c Context) EmptyFields() Context {
389+
c.l.context = enc.AppendBeginMarker(make([]byte, 0, 500))
390+
return c
391+
}
392+
387393
type callerHook struct {
388394
callerSkipFrameCount int
389395
}

log_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ func TestWith(t *testing.T) {
145145
}
146146
}
147147

148+
func TestWithEmptyFields(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+
EmptyFields().
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)