WriteAll writes all accumulated Log entries and resets the batch queue.
(ctx context.Context)
| 200 | |
| 201 | // WriteAll writes all accumulated Log entries and resets the batch queue. |
| 202 | func (h *BatchHandler) WriteAll(ctx context.Context) error { |
| 203 | if h.parent != nil { |
| 204 | // invoke recursively the parent level handler since the most |
| 205 | // top level one is holding the logs queue. |
| 206 | return h.parent.WriteAll(ctx) |
| 207 | } |
| 208 | |
| 209 | h.mux.Lock() |
| 210 | |
| 211 | totalLogs := len(h.logs) |
| 212 | |
| 213 | // no logs to write |
| 214 | if totalLogs == 0 { |
| 215 | h.mux.Unlock() |
| 216 | return nil |
| 217 | } |
| 218 | |
| 219 | // create a copy of the logs slice to prevent blocking during write |
| 220 | logs := make([]*Log, totalLogs) |
| 221 | copy(logs, h.logs) |
| 222 | h.logs = h.logs[:0] // reset |
| 223 | |
| 224 | h.mux.Unlock() |
| 225 | |
| 226 | return h.options.WriteFunc(ctx, logs) |
| 227 | } |
| 228 | |
| 229 | // resolveAttr writes attr into data. |
| 230 | func (h *BatchHandler) resolveAttr(data map[string]any, attr slog.Attr) error { |