From 5e5958d22e3fe6e6a14c72964f20ffddf56f8a79 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 7 Sep 2023 13:55:32 +0000 Subject: [PATCH] zones: introduce (private) logger interface Signed-off-by: Alejandro Mery --- pkg/zones/log.go | 49 ++++++++++++++++++++++++++++++++++++++++++++ pkg/zones/options.go | 1 + pkg/zones/zones.go | 2 ++ 3 files changed, 52 insertions(+) create mode 100644 pkg/zones/log.go diff --git a/pkg/zones/log.go b/pkg/zones/log.go new file mode 100644 index 0000000..ceaf5ee --- /dev/null +++ b/pkg/zones/log.go @@ -0,0 +1,49 @@ +package zones + +import "darvaza.org/slog" + +type logger interface { + withDebug() (slog.Logger, bool) + withInfo() (slog.Logger, bool) + + debug() slog.Logger + info() slog.Logger + warn(error) slog.Logger + error(error) slog.Logger +} + +var ( + _ logger = (*Zones)(nil) +) + +func (z *Zones) withDebug() (slog.Logger, bool) { + return z.debug().WithEnabled() +} + +func (z *Zones) withInfo() (slog.Logger, bool) { + return z.debug().WithEnabled() +} + +func (z *Zones) debug() slog.Logger { + return z.log.Debug() +} + +func (z *Zones) info() slog.Logger { + return z.log.Info() +} + +func (z *Zones) warn(err error) slog.Logger { + l := z.log.Warn() + if err != nil { + l = l.WithField(slog.ErrorFieldName, err) + } + return l +} + +func (z *Zones) error(err error) slog.Logger { + l := z.log.Error() + if err != nil { + l = l.WithField(slog.ErrorFieldName, err) + } + return l +} diff --git a/pkg/zones/options.go b/pkg/zones/options.go index faefb9f..e7a66f2 100644 --- a/pkg/zones/options.go +++ b/pkg/zones/options.go @@ -69,6 +69,7 @@ func WithLogger(log slog.Logger) ScanOption { } opt.Logger = log + m.log = log return nil } } diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index fad4c30..89d2509 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -6,6 +6,7 @@ import ( "sort" "darvaza.org/resolver" + "darvaza.org/slog" "github.com/gofrs/uuid/v5" ) @@ -141,6 +142,7 @@ func (z *Zone) GatewayIDs() ([]int, int) { // Zones represents all zones in a cluster type Zones struct { dir fs.FS + log slog.Logger resolver resolver.Resolver domain string