From 20be557b7bc3d7ada78411b7b279c9f7580d0bc7 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 9 Oct 2023 08:58:31 -0600 Subject: logging.Base() Allow users to get access to the base logger *before* it is wrapped with the various levels. This provides a single override point to filter or redirect all logging. --- internal/server.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'internal/server.go') diff --git a/internal/server.go b/internal/server.go index a95efc1..21d1a7f 100644 --- a/internal/server.go +++ b/internal/server.go @@ -6,20 +6,18 @@ import ( "strings" "sync" + "github.com/go-kit/log" + "github.com/go-kit/log/level" "tildegit.org/tjp/sliderule/logging" ) -type logger interface { - Log(keyvals ...any) error -} - type Server struct { Ctx context.Context Cancel context.CancelFunc Wg *sync.WaitGroup Listener net.Listener HandleConn connHandler - ErrorLog logger + ErrorLog logging.Logger Host string NetworkAddr net.Addr } @@ -31,7 +29,7 @@ func NewServer( hostname string, network string, address string, - errorLog logger, + baseLog logging.Logger, handleConn connHandler, ) (Server, error) { listener, err := net.Listen(network, address) @@ -42,10 +40,13 @@ func NewServer( networkAddr := listener.Addr() ctx, cancel := context.WithCancel(ctx) - debuglog, infolog, warnlog, errlog := logging.DefaultLoggers() - ctx = context.WithValue(ctx, "debuglog", debuglog) - ctx = context.WithValue(ctx, "infolog", infolog) - ctx = context.WithValue(ctx, "warnlog", warnlog) + if baseLog == nil { + baseLog = log.NewNopLogger() + } + errlog := level.Error(baseLog) + ctx = context.WithValue(ctx, "debuglog", level.Debug(baseLog)) + ctx = context.WithValue(ctx, "infolog", level.Info(baseLog)) + ctx = context.WithValue(ctx, "warnlog", level.Warn(baseLog)) ctx = context.WithValue(ctx, "errlog", errlog) return Server{ @@ -54,7 +55,7 @@ func NewServer( Wg: &sync.WaitGroup{}, Listener: listener, HandleConn: handleConn, - ErrorLog: errorLog, + ErrorLog: errlog, Host: hostname, NetworkAddr: networkAddr, }, nil -- cgit v1.2.3