diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-10-09 08:58:31 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-10-09 08:58:31 -0600 |
commit | 20be557b7bc3d7ada78411b7b279c9f7580d0bc7 (patch) | |
tree | 2a7f6640b0b389ad79fe18a8906642be493aaf60 /internal/server.go | |
parent | cedcf58ea7d729acb6ed1a9ab7aec1ae38aed102 (diff) |
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.
Diffstat (limited to 'internal/server.go')
-rw-r--r-- | internal/server.go | 23 |
1 files changed, 12 insertions, 11 deletions
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 |