...

Source file src/cdr.dev/slog/example_marshaller_test.go

Documentation: cdr.dev/slog

     1  package slog_test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	"cdr.dev/slog"
     8  	"cdr.dev/slog/sloggers/sloghuman"
     9  )
    10  
    11  type myStruct struct {
    12  	foo int
    13  	bar int
    14  }
    15  
    16  func (s myStruct) MarshalJSON() ([]byte, error) {
    17  	return slog.M(
    18  		slog.F("foo", s.foo),
    19  		slog.F("bar", s.foo),
    20  	).MarshalJSON()
    21  }
    22  
    23  func Example_marshaller() {
    24  	l := slog.Make(sloghuman.Sink(os.Stdout))
    25  
    26  	l.Info(context.Background(), "wow",
    27  		slog.F("myStruct", myStruct{
    28  			foo: 1,
    29  			bar: 2,
    30  		}),
    31  	)
    32  
    33  	// 2019-12-16 17:31:37.120 [INFO]	<example_marshaller_test.go:26>	wow	{"myStruct": {"foo": 1, "bar": 1}}
    34  }
    35  

View as plain text