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

Function ExampleContext_Objects

binary_test.go:295–312  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

293}
294
295func ExampleContext_Objects() {
296 // In go, arrays are type invariant so even if you have a variable u of type []User array and User implements
297 // the LogObjectMarshaler interface, you cannot pass that to func that takes an []LogObjectMarshaler array in the
298 // Objects call. In 1.24+ it allows passing the variadic covariant slice (e.g. u...) but the unit test needs to
299 // work in earlier versions so we'll declare the array as []LogObjectMarshaler here.
300 u := []LogObjectMarshaler{User{"John", 35, time.Time{}}, User{"Bob", 55, time.Time{}}}
301
302 dst := bytes.Buffer{}
303 log := New(&dst).With().
304 Str("foo", "bar").
305 Objects("users", u).
306 Logger()
307
308 log.Log().Msg("hello world")
309
310 fmt.Println(decodeIfBinaryToString(dst.Bytes()))
311 // Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
312}
313
314func ExampleEvent_EmbedObject() {
315 price := Price{val: 6449, prec: 2, unit: "$"}

Callers

nothing calls this directly

Calls 10

NewFunction · 0.85
LoggerMethod · 0.80
WithMethod · 0.80
MsgMethod · 0.80
PrintlnMethod · 0.80
decodeIfBinaryToStringFunction · 0.70
LogMethod · 0.65
ObjectsMethod · 0.45
StrMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…