MCPcopy
hub / github.com/rs/zerolog / TestFields

Function TestFields

log_test.go:478–526  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

476}
477
478func TestFields(t *testing.T) {
479 out := &bytes.Buffer{}
480 log := New(out)
481 now := time.Now()
482 _, file, line, _ := runtime.Caller(0)
483 caller := fmt.Sprintf("%s:%d", file, line+3)
484 log.Log().
485 Caller().
486 Object("obj", fixtureObj{"a", "z", 1}).
487 Str("string", "foo").
488 Stringer("stringer", net.IP{127, 0, 0, 1}).
489 Bytes("bytes", []byte("bar")).
490 Hex("hex", []byte{0x12, 0xef}).
491 RawJSON("json", []byte(`{"some":"json"}`)).
492 RawCBOR("cbor", []byte{0x83, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05}).
493 Func(func(e *Event) { e.Str("func", "func_output") }).
494 AnErr("some_err", nil).
495 Err(errors.New("some error")).
496 Bool("bool", true).
497 Int("int", 1).
498 Int8("int8", 2).
499 Int16("int16", 3).
500 Int32("int32", 4).
501 Int64("int64", 5).
502 Uint("uint", 6).
503 Uint8("uint8", 7).
504 Uint16("uint16", 8).
505 Uint32("uint32", 9).
506 Uint64("uint64", 10).
507 IPAddr("ipv4", net.IP{192, 168, 0, 100}).
508 IPAddr("ipv6", net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34}).
509 MACAddr("mac", net.HardwareAddr{0x00, 0x14, 0x22, 0x01, 0x23, 0x45}).
510 IPPrefix("pfxv4", net.IPNet{IP: net.IP{192, 168, 0, 100}, Mask: net.CIDRMask(24, 32)}).
511 IPPrefix("pfxv6", net.IPNet{IP: net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34}, Mask: net.CIDRMask(64, 128)}).
512 Float32("float32", 11.1234).
513 Float64("float64", 12.321321321).
514 Dur("dur", 1*time.Second).
515 Time("time", time.Time{}).
516 TimeDiff("diff", now, now.Add(-10*time.Second)).
517 Ctx(context.Background()).
518 Type("type", "hello").
519 Any("any", struct{ A string }{"test"}).
520 Any("logobject", fixtureObj{"a", "z", 1}).
521 Stack().
522 Msg("")
523 if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","obj":{"Pub":"a","Tag":"z","priv":1},"string":"foo","stringer":"127.0.0.1","bytes":"bar","hex":"12ef","json":{"some":"json"},"cbor":"data:application/cbor;base64,gwGCAgOCBAU=","func":"func_output","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,"ipv4":"192.168.0.100","ipv6":"2001:db8:85a3::8a2e:370:7334","mac":"00:14:22:01:23:45","pfxv4":"192.168.0.100/24","pfxv6":"2001:db8:85a3::8a2e:370:7334/64","float32":11.1234,"float64":12.321321321,"dur":1000,"time":"0001-01-01T00:00:00Z","diff":10000,"type":"string","any":{"A":"test"},"logobject":{"Pub":"a","Tag":"z","priv":1}}`+"\n"; got != want {
524 t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
525 }
526}
527
528func TestFieldsArrayNil(t *testing.T) {
529 out := &bytes.Buffer{}

Callers

nothing calls this directly

Calls 15

NewFunction · 0.85
MsgMethod · 0.80
TimeDiffMethod · 0.80
FuncMethod · 0.80
RawCBORMethod · 0.80
decodeIfBinaryToStringFunction · 0.70
ErrMethod · 0.65
LogMethod · 0.65
CallerMethod · 0.45
StackMethod · 0.45
AnyMethod · 0.45
TypeMethod · 0.45

Tested by

no test coverage detected