Handle processes a log record.
(ctx context.Context, r slog.Record)
| 130 | |
| 131 | // Handle processes a log record. |
| 132 | func (h *Handler) Handle(ctx context.Context, r slog.Record) error { |
| 133 | buf := newBuffer() |
| 134 | defer func() { |
| 135 | buf.free() |
| 136 | }() |
| 137 | |
| 138 | h.format(r, buf) |
| 139 | |
| 140 | h.mu.Lock() |
| 141 | defer h.mu.Unlock() |
| 142 | |
| 143 | var errs []error |
| 144 | |
| 145 | // Write log entry to output |
| 146 | if r.Level >= h.config.Level.Level() { |
| 147 | _, err := h.out.Write(*buf) |
| 148 | if err != nil { |
| 149 | errs = append(errs, err) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Fire hooks |
| 154 | for _, hook := range h.hooks { |
| 155 | if !hook.Enabled(r.Level) { |
| 156 | continue |
| 157 | } |
| 158 | if err := hook.Fire(ctx, r.Time, r.Level, slices.Clip(*buf)); err != nil { |
| 159 | errs = append(errs, err) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // If writing to output or firing hooks returned errors, |
| 164 | // join them, write to STDERR, and return |
| 165 | if err := h.joinErrors(errs); err != nil { |
| 166 | h.writeError(err) |
| 167 | return err |
| 168 | } |
| 169 | |
| 170 | return nil |
| 171 | } |
| 172 | |
| 173 | // format formats a log record and writes it to the buffer. |
| 174 | func (h *Handler) format(r slog.Record, buf *buffer) { |
no test coverage detected