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

Function NewWriter

diode/diode.go:51–72  ·  view source on GitHub ↗

NewWriter creates a writer wrapping w with a many-to-one diode in order to never block log producers and drop events if the writer can't keep up with the flow of data. Use a diode.Writer when wr := diode.NewWriter(w, 1000, 0, func(missed int) { log.Printf("Dropped %d messages", missed) }) log := z

(w io.Writer, size int, pollInterval time.Duration, f Alerter)

Source from the content-addressed store, hash-verified

49//
50// See code.cloudfoundry.org/go-diodes for more info on diode.
51func NewWriter(w io.Writer, size int, pollInterval time.Duration, f Alerter) Writer {
52 ctx, cancel := context.WithCancel(context.Background())
53 dw := Writer{
54 w: w,
55 c: cancel,
56 done: make(chan struct{}),
57 }
58 if f == nil {
59 f = func(int) {}
60 }
61 d := diodes.NewManyToOne(size, diodes.AlertFunc(f))
62 if pollInterval > 0 {
63 dw.d = diodes.NewPoller(d,
64 diodes.WithPollingInterval(pollInterval),
65 diodes.WithPollingContext(ctx))
66 } else {
67 dw.d = diodes.NewWaiter(d,
68 diodes.WithWaiterContext(ctx))
69 }
70 go dw.poll()
71 return dw
72}
73
74func (dw Writer) Write(p []byte) (n int, err error) {
75 // p is pooled in zerolog so we can't hold it passed this call, hence the

Callers 6

TestNewWriterFunction · 0.92
TestCloseFunction · 0.92
TestFatalFunction · 0.92
BenchmarkFunction · 0.92
ExampleNewWriterFunction · 0.92

Calls 8

pollMethod · 0.95
NewManyToOneFunction · 0.92
AlertFuncFuncType · 0.92
NewPollerFunction · 0.92
WithPollingIntervalFunction · 0.92
WithPollingContextFunction · 0.92
NewWaiterFunction · 0.92
WithWaiterContextFunction · 0.92

Tested by 6

TestNewWriterFunction · 0.74
TestCloseFunction · 0.74
TestFatalFunction · 0.74
BenchmarkFunction · 0.74
ExampleNewWriterFunction · 0.74