Handle implements the slog.Handler interface.
(ctx context.Context, r slog.Record)
| 129 | |
| 130 | // Handle implements the slog.Handler interface. |
| 131 | func (c *consoleHandler) Handle(ctx context.Context, r slog.Record) error { |
| 132 | c.lock.Lock() |
| 133 | defer c.lock.Unlock() |
| 134 | c.buffer.Reset() |
| 135 | if c.enableColor { |
| 136 | c.buffer.WriteString(colorize(r.Level.String(), getColor(r.Level))) |
| 137 | } else { |
| 138 | c.buffer.WriteString(r.Level.String()) |
| 139 | } |
| 140 | c.buffer.WriteString(consoleSeparator) |
| 141 | c.buffer.WriteString(r.Message) |
| 142 | bufN := c.buffer.Len() |
| 143 | c.buffer.WriteString(consoleSeparator) |
| 144 | // Delegate must always be called, as it may have attributes to write. |
| 145 | if err := c.delegate.Handle(ctx, r); err != nil { |
| 146 | return err |
| 147 | } |
| 148 | if c.buffer.Len() == bufN+len(consoleSeparator+"{}\n") { |
| 149 | // No attributes to write, trim the buffer to remove the empty JSON object. |
| 150 | c.buffer.Truncate(bufN) |
| 151 | c.buffer.WriteByte('\n') |
| 152 | } |
| 153 | _, err := c.buffer.WriteTo(c.out) |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | // WithAttrs implements the slog.Handler interface. |
| 158 | func (c *consoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler { |