mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-08-08 23:12:18 -04:00
move LevelHandler to own file
This commit is contained in:
parent
7f0c697592
commit
3e6375e763
2 changed files with 51 additions and 45 deletions
51
internal/logger/levelhandler.go
Normal file
51
internal/logger/levelhandler.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"context"
|
||||
)
|
||||
|
||||
// LevelHandler copied from the official LevelHandler example in the slog package documentation.
|
||||
|
||||
// LevelHandler wraps a Handler with an Enabled method
|
||||
// that returns false for levels below a minimum.
|
||||
type LevelHandler struct {
|
||||
level slog.Leveler
|
||||
handler slog.Handler
|
||||
}
|
||||
|
||||
// NewLevelHandler returns a LevelHandler with the given level.
|
||||
// All methods except Enabled delegate to h.
|
||||
func NewLevelHandler(level slog.Leveler, h slog.Handler) *LevelHandler {
|
||||
// Optimization: avoid chains of LevelHandlers.
|
||||
if lh, ok := h.(*LevelHandler); ok {
|
||||
h = lh.Handler()
|
||||
}
|
||||
return &LevelHandler{level, h}
|
||||
}
|
||||
|
||||
// Enabled implements Handler.Enabled by reporting whether
|
||||
// level is at least as large as h's level.
|
||||
func (h *LevelHandler) Enabled(_ context.Context, level slog.Level) bool {
|
||||
return level >= h.level.Level()
|
||||
}
|
||||
|
||||
// Handle implements Handler.Handle.
|
||||
func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
return h.handler.Handle(ctx, r)
|
||||
}
|
||||
|
||||
// WithAttrs implements Handler.WithAttrs.
|
||||
func (h *LevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return NewLevelHandler(h.level, h.handler.WithAttrs(attrs))
|
||||
}
|
||||
|
||||
// WithGroup implements Handler.WithGroup.
|
||||
func (h *LevelHandler) WithGroup(name string) slog.Handler {
|
||||
return NewLevelHandler(h.level, h.handler.WithGroup(name))
|
||||
}
|
||||
|
||||
// Handler returns the Handler wrapped by h.
|
||||
func (h *LevelHandler) Handler() slog.Handler {
|
||||
return h.handler
|
||||
}
|
|
@ -109,51 +109,6 @@ func middlewareLogger(l *slog.Logger) logging.Logger {
|
|||
})
|
||||
}
|
||||
|
||||
// LevelHandler copied from the official LevelHandler example in the slog package documentation.
|
||||
|
||||
// LevelHandler wraps a Handler with an Enabled method
|
||||
// that returns false for levels below a minimum.
|
||||
type LevelHandler struct {
|
||||
level slog.Leveler
|
||||
handler slog.Handler
|
||||
}
|
||||
|
||||
// NewLevelHandler returns a LevelHandler with the given level.
|
||||
// All methods except Enabled delegate to h.
|
||||
func NewLevelHandler(level slog.Leveler, h slog.Handler) *LevelHandler {
|
||||
// Optimization: avoid chains of LevelHandlers.
|
||||
if lh, ok := h.(*LevelHandler); ok {
|
||||
h = lh.Handler()
|
||||
}
|
||||
return &LevelHandler{level, h}
|
||||
}
|
||||
|
||||
// Enabled implements Handler.Enabled by reporting whether
|
||||
// level is at least as large as h's level.
|
||||
func (h *LevelHandler) Enabled(_ context.Context, level slog.Level) bool {
|
||||
return level >= h.level.Level()
|
||||
}
|
||||
|
||||
// Handle implements Handler.Handle.
|
||||
func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
return h.handler.Handle(ctx, r)
|
||||
}
|
||||
|
||||
// WithAttrs implements Handler.WithAttrs.
|
||||
func (h *LevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return NewLevelHandler(h.level, h.handler.WithAttrs(attrs))
|
||||
}
|
||||
|
||||
// WithGroup implements Handler.WithGroup.
|
||||
func (h *LevelHandler) WithGroup(name string) slog.Handler {
|
||||
return NewLevelHandler(h.level, h.handler.WithGroup(name))
|
||||
}
|
||||
|
||||
// Handler returns the Handler wrapped by h.
|
||||
func (h *LevelHandler) Handler() slog.Handler {
|
||||
return h.handler
|
||||
}
|
||||
|
||||
func NewTest(t * testing.T) *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(testWriter{t: t}, nil))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue