From 4e3a6fd348a3c764fff5193cd0ee34eea4402318 Mon Sep 17 00:00:00 2001 From: TJ Hoplock <33664289+tjhop@users.noreply.github.com> Date: Fri, 27 Sep 2024 17:19:05 -0400 Subject: [PATCH] feat: add `promslog.NewNopLogger()` convenience func (#697) 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)) +}