From f15716605ff33d3b6f5f81f8228ed335ce53e642 Mon Sep 17 00:00:00 2001 From: TJ Hoplock Date: Fri, 27 Sep 2024 00:30:13 -0400 Subject: [PATCH] feat: add `promslog.NewNopLogger()` convenience func Simple convenience function to return an slog.Logger that writes to io.Discard. Originally suggested by @ArthurSens [here](https://github.com/prometheus/common/pull/686#issuecomment-2336501004), and requested again by @bboreham [here](https://github.com/prometheus/prometheus/pull/14906#discussion_r1770597610). As Bryan points out in the comment, there's 147 instances where a discard logger is needed, so a consistent utility function to manage them seems helpful. Signed-off-by: TJ Hoplock --- promslog/slog.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/promslog/slog.go b/promslog/slog.go index fae3684b..31e003be 100644 --- a/promslog/slog.go +++ b/promslog/slog.go @@ -180,3 +180,9 @@ func New(config *Config) *slog.Logger { } return slog.New(slog.NewTextHandler(config.Writer, logHandlerOpts)) } + +// NewNopLogger is a convenience function to return an slog.Logger that writes +// to io.Discard. +func NewNopLogger() *slog.Logger { + return slog.New(slog.NewTextHandler(io.Discard, nil)) +}