tlog.app/go/tlog@v0.23.1/examples/dumper/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "io" 6 "strings" 7 8 "tlog.app/go/loc" 9 "tlog.app/go/tlog" 10 "tlog.app/go/tlog/tlio" 11 ) 12 13 func main() { 14 cw := tlog.NewConsoleWriter(tlog.Stderr, tlog.LdetFlags) 15 tlog.DefaultLogger = tlog.New(cw) 16 17 cw.StringOnNewLineMinLen = 10 18 19 q := "some file content or socket...\nor some other data you want to dump to logs" 20 21 w := io.Discard // just for example 22 23 _ = copyStream(context.Background(), w, strings.NewReader(q)) 24 25 // tlog.Printw("msg", "obj", map[string]string{"key": q, "key2": q}) 26 } 27 28 func copyStream(ctx context.Context, w io.Writer, r io.Reader) (err error) { 29 tr := tlog.SpawnFromContextOrStart(ctx, "copy_stream") 30 defer func() { tr.Finish("err", err, tlog.KeyCaller, loc.Caller(1)) }() 31 32 // Here is the trick 33 lw := tlio.WriterFunc(func(p []byte) (int, error) { 34 tr.Printw("copied block", "len", tlog.NextAsHex, len(p), "block", p) 35 36 return len(p), nil 37 }) 38 39 w = io.MultiWriter(w, lw) 40 41 r = tlio.NopCloser{Reader: r} // preserve 20 bytes buffer, prevent WriteTo fastpath 42 43 _, err = io.CopyBuffer(w, r, make([]byte, 20)) 44 45 return err 46 }