mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 12:03:33 +00:00
fix SlogTextHandler to respect minimum log level
This commit is contained in:
+13
-2
@@ -16,8 +16,9 @@ import (
|
|||||||
// LEVEL MESSAGE key1=value1 key2=value2
|
// LEVEL MESSAGE key1=value1 key2=value2
|
||||||
type SlogTextHandler struct {
|
type SlogTextHandler struct {
|
||||||
*slog.TextHandler
|
*slog.TextHandler
|
||||||
mu sync.Mutex
|
opts slog.HandlerOptions
|
||||||
w io.Writer
|
mu sync.Mutex
|
||||||
|
w io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSlogTextHandler(w io.Writer, opts *slog.HandlerOptions) *SlogTextHandler {
|
func NewSlogTextHandler(w io.Writer, opts *slog.HandlerOptions) *SlogTextHandler {
|
||||||
@@ -34,6 +35,7 @@ func NewSlogTextHandler(w io.Writer, opts *slog.HandlerOptions) *SlogTextHandler
|
|||||||
|
|
||||||
return &SlogTextHandler{
|
return &SlogTextHandler{
|
||||||
TextHandler: slog.NewTextHandler(w, opts),
|
TextHandler: slog.NewTextHandler(w, opts),
|
||||||
|
opts: *opts,
|
||||||
w: w,
|
w: w,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,6 +53,15 @@ func (h *SlogTextHandler) WithGroup(name string) slog.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *SlogTextHandler) Handle(ctx context.Context, r slog.Record) error {
|
func (h *SlogTextHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||||
|
// Only log entries that are at or above the minimum level (INFO by default).
|
||||||
|
minLevel := slog.LevelInfo
|
||||||
|
if h.opts.Level != nil {
|
||||||
|
minLevel = h.opts.Level.Level()
|
||||||
|
}
|
||||||
|
if r.Level < minLevel {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
defer h.mu.Unlock()
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user