Merge pull request #15205 from tjhop/chore/slog-fixes

slog: various fixes
pull/12659/head
Jan Fajerski 2024-10-25 11:11:46 +02:00 committed by GitHub
commit 24a10528ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 36 deletions

View File

@ -51,11 +51,7 @@ func Dedupe(next *slog.Logger, repeat time.Duration) *Deduper {
// provided context and log level, and returns false otherwise. It implements // provided context and log level, and returns false otherwise. It implements
// slog.Handler. // slog.Handler.
func (d *Deduper) Enabled(ctx context.Context, level slog.Level) bool { func (d *Deduper) Enabled(ctx context.Context, level slog.Level) bool {
d.mtx.RLock() return d.next.Enabled(ctx, level)
enabled := d.next.Enabled(ctx, level)
d.mtx.RUnlock()
return enabled
} }
// Handle uses the provided context and slog.Record to deduplicate messages // Handle uses the provided context and slog.Record to deduplicate messages
@ -85,43 +81,27 @@ func (d *Deduper) Handle(ctx context.Context, r slog.Record) error {
// WithAttrs adds the provided attributes to the Deduper's internal // WithAttrs adds the provided attributes to the Deduper's internal
// slog.Logger. It implements slog.Handler. // slog.Logger. It implements slog.Handler.
func (d *Deduper) WithAttrs(attrs []slog.Attr) slog.Handler { func (d *Deduper) WithAttrs(attrs []slog.Attr) slog.Handler {
d.mtx.Lock() return &Deduper{
d.next = slog.New(d.next.Handler().WithAttrs(attrs)) next: slog.New(d.next.Handler().WithAttrs(attrs)),
d.mtx.Unlock() repeat: d.repeat,
return d quit: d.quit,
seen: d.seen,
}
} }
// WithGroup adds the provided group name to the Deduper's internal // WithGroup adds the provided group name to the Deduper's internal
// slog.Logger. It implements slog.Handler. // slog.Logger. It implements slog.Handler.
func (d *Deduper) WithGroup(name string) slog.Handler { func (d *Deduper) WithGroup(name string) slog.Handler {
d.mtx.Lock() if name == "" {
d.next = slog.New(d.next.Handler().WithGroup(name)) return d
d.mtx.Unlock() }
return d
}
// Info logs the provided message and key-value arguments using the Deduper's return &Deduper{
// internal slog.Logger. It is simply a wrapper around slog.Logger.Info(). next: slog.New(d.next.Handler().WithGroup(name)),
func (d *Deduper) Info(msg string, args ...any) { repeat: d.repeat,
d.next.Info(msg, args...) quit: d.quit,
} seen: d.seen,
}
// Warn logs the provided message and key-value arguments using the Deduper's
// internal slog.Logger. It is simply a wrapper around slog.Logger.Warn().
func (d *Deduper) Warn(msg string, args ...any) {
d.next.Warn(msg, args...)
}
// Error logs the provided message and key-value arguments using the Deduper's
// internal slog.Logger. It is simply a wrapper around slog.Logger.Error().
func (d *Deduper) Error(msg string, args ...any) {
d.next.Error(msg, args...)
}
// Debug logs the provided message and key-value arguments using the Deduper's
// internal slog.Logger. It is simply a wrapper around slog.Logger.Debug().
func (d *Deduper) Debug(msg string, args ...any) {
d.next.Debug(msg, args...)
} }
// Stop the Deduper. // Stop the Deduper.