NewBatchHandler creates a slog compatible handler that writes JSON logs on batches (default to 100), using the given options. Panics if [BatchOptions.WriteFunc] is not defined. Example: l := slog.New(logger.NewBatchHandler(logger.BatchOptions{ WriteFunc: func(ctx context.Context, logs []*Lo
(options BatchOptions)
| 51 | // })) |
| 52 | // l.Info("Example message", "title", "lorem ipsum") |
| 53 | func NewBatchHandler(options BatchOptions) *BatchHandler { |
| 54 | h := &BatchHandler{ |
| 55 | mux: &sync.Mutex{}, |
| 56 | options: &options, |
| 57 | } |
| 58 | |
| 59 | if h.options.WriteFunc == nil { |
| 60 | panic("options.WriteFunc must be set") |
| 61 | } |
| 62 | |
| 63 | if h.options.Level == nil { |
| 64 | h.options.Level = slog.LevelInfo |
| 65 | } |
| 66 | |
| 67 | if h.options.BatchSize == 0 { |
| 68 | h.options.BatchSize = 100 |
| 69 | } |
| 70 | |
| 71 | h.logs = make([]*Log, 0, h.options.BatchSize) |
| 72 | |
| 73 | return h |
| 74 | } |
| 75 | |
| 76 | // BatchHandler is a slog handler that writes records on batches. |
| 77 | // |
no outgoing calls
searching dependent graphs…