mirror of
https://github.com/alexgo-io/gaze-indexer.git
synced 2026-04-29 12:15:13 +08:00
feat: add log multi middleware handler
Co-authored-by: Gaze <gazenw@users.noreply.github.com>
This commit is contained in:
@@ -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),
|
||||
}
|
||||
Reference in New Issue
Block a user