feat: add log multi middleware handler

Co-authored-by: Gaze <gazenw@users.noreply.github.com>
This commit is contained in:
Gaze
2024-04-10 03:43:19 +07:00
parent dc60975437
commit 78ef151903

View File

@@ -10,23 +10,23 @@ type (
middleware func(handleFunc) handleFunc
)
type chainHandlers struct {
type multiHandlers struct {
h slog.Handler
middlewares []middleware
}
func newChainHandlers(handler slog.Handler, middlewares ...middleware) *chainHandlers {
return &chainHandlers{
func newChainHandlers(handler slog.Handler, middlewares ...middleware) *multiHandlers {
return &multiHandlers{
h: handler,
middlewares: middlewares,
}
}
func (c *chainHandlers) Enabled(ctx context.Context, lvl slog.Level) bool {
func (c *multiHandlers) Enabled(ctx context.Context, lvl slog.Level) bool {
return c.h.Enabled(ctx, lvl)
}
func (c *chainHandlers) Handle(ctx context.Context, rec slog.Record) error {
func (c *multiHandlers) Handle(ctx context.Context, rec slog.Record) error {
h := c.h.Handle
for i := len(c.middlewares) - 1; i >= 0; i-- {
h = c.middlewares[i](h)
@@ -34,15 +34,15 @@ func (c *chainHandlers) Handle(ctx context.Context, rec slog.Record) error {
return h(ctx, rec)
}
func (c *chainHandlers) WithGroup(group string) slog.Handler {
return &chainHandlers{
func (c *multiHandlers) WithGroup(group string) slog.Handler {
return &multiHandlers{
middlewares: c.middlewares,
h: c.h.WithGroup(group),
}
}
func (c *chainHandlers) WithAttrs(attrs []slog.Attr) slog.Handler {
return &chainHandlers{
func (c *multiHandlers) WithAttrs(attrs []slog.Attr) slog.Handler {
return &multiHandlers{
middlewares: c.middlewares,
h: c.h.WithAttrs(attrs),
}