Add RawJSON field type

This commit is contained in:
Olivier Poitrey 2018-02-12 16:05:27 -08:00
parent 9a92fd2536
commit 56a970de51
3 changed files with 22 additions and 2 deletions

View File

@ -79,6 +79,15 @@ func (c Context) Bytes(key string, val []byte) Context {
return c return c
} }
// RawJSON adds already encoded JSON to context.
//
// No sanity check is performed on b; it must not contain carriage returns and
// be valid JSON.
func (c Context) RawJSON(key string, b []byte) Context {
c.l.context = append(json.AppendKey(c.l.context, key), b...)
return c
}
// AnErr adds the field key with err as a string to the logger context. // AnErr adds the field key with err as a string to the logger context.
func (c Context) AnErr(key string, err error) Context { func (c Context) AnErr(key string, err error) Context {
if err != nil { if err != nil {

View File

@ -218,6 +218,15 @@ func (e *Event) Bytes(key string, val []byte) *Event {
return e return e
} }
// RawJSON adds already encoded JSON to the log line under key.
//
// No sanity check is performed on b; it must not contain carriage returns and
// be valid JSON.
func (e *Event) RawJSON(key string, b []byte) *Event {
e.buf = append(json.AppendKey(e.buf, key), b...)
return e
}
// AnErr adds the field key with err as a string to the *Event context. // AnErr adds the field key with err as a string to the *Event context.
// If err is nil, no field is added. // If err is nil, no field is added.
func (e *Event) AnErr(key string, err error) *Event { func (e *Event) AnErr(key string, err error) *Event {

View File

@ -78,6 +78,7 @@ func TestWith(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
ctx := New(out).With(). ctx := New(out).With().
Str("foo", "bar"). Str("foo", "bar").
RawJSON("json", []byte(`{"some":"json"}`)).
AnErr("some_err", nil). AnErr("some_err", nil).
Err(errors.New("some error")). Err(errors.New("some error")).
Bool("bool", true). Bool("bool", true).
@ -98,7 +99,7 @@ func TestWith(t *testing.T) {
caller := fmt.Sprintf("%s:%d", file, line+3) caller := fmt.Sprintf("%s:%d", file, line+3)
log := ctx.Caller().Logger() log := ctx.Caller().Logger()
log.Log().Msg("") log.Log().Msg("")
if got, want := out.String(), `{"foo":"bar","error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11,"float64":12,"time":"0001-01-01T00:00:00Z","caller":"`+caller+`"}`+"\n"; got != want { if got, want := out.String(), `{"foo":"bar","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11,"float64":12,"time":"0001-01-01T00:00:00Z","caller":"`+caller+`"}`+"\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want) t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
} }
} }
@ -142,6 +143,7 @@ func TestFields(t *testing.T) {
Caller(). Caller().
Str("string", "foo"). Str("string", "foo").
Bytes("bytes", []byte("bar")). Bytes("bytes", []byte("bar")).
RawJSON("json", []byte(`{"some":"json"}`)).
AnErr("some_err", nil). AnErr("some_err", nil).
Err(errors.New("some error")). Err(errors.New("some error")).
Bool("bool", true). Bool("bool", true).
@ -161,7 +163,7 @@ func TestFields(t *testing.T) {
Time("time", time.Time{}). Time("time", time.Time{}).
TimeDiff("diff", now, now.Add(-10*time.Second)). TimeDiff("diff", now, now.Add(-10*time.Second)).
Msg("") Msg("")
if got, want := out.String(), `{"caller":"`+caller+`","string":"foo","bytes":"bar","error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11,"float64":12,"dur":1000,"time":"0001-01-01T00:00:00Z","diff":10000}`+"\n"; got != want { if got, want := out.String(), `{"caller":"`+caller+`","string":"foo","bytes":"bar","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11,"float64":12,"dur":1000,"time":"0001-01-01T00:00:00Z","diff":10000}`+"\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want) t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
} }
} }