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 | |
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.
-rw-r--r-- | examples/cgi/main.go | 7 | ||||
-rw-r--r-- | examples/cowsay/main.go | 7 | ||||
-rw-r--r-- | examples/fileserver/main.go | 7 | ||||
-rw-r--r-- | examples/finger/main.go | 8 | ||||
-rw-r--r-- | examples/gopher_fileserver/main.go | 7 | ||||
-rw-r--r-- | examples/inspectls/main.go | 7 | ||||
-rw-r--r-- | finger/serve.go | 4 | ||||
-rw-r--r-- | gemini/serve.go | 4 | ||||
-rw-r--r-- | gopher/serve.go | 4 | ||||
-rw-r--r-- | internal/server.go | 23 | ||||
-rw-r--r-- | logging/logger.go | 54 | ||||
-rw-r--r-- | spartan/serve.go | 4 |
12 files changed, 77 insertions, 59 deletions
diff --git a/examples/cgi/main.go b/examples/cgi/main.go index 4d48422..587c874 100644 --- a/examples/cgi/main.go +++ b/examples/cgi/main.go @@ -7,6 +7,7 @@ import ( "os/signal" "syscall" + "github.com/go-kit/log/level" "tildegit.org/tjp/sliderule/contrib/cgi" "tildegit.org/tjp/sliderule/gemini" "tildegit.org/tjp/sliderule/logging" @@ -25,17 +26,17 @@ func main() { // make use of a CGI request handler cgiHandler := cgi.GeminiCGIDirectory("./cgi-bin", "/cgi-bin") - _, infoLog, _, errLog := logging.DefaultLoggers() + baseLog := logging.Base() // add stdout logging to the request handler - handler := logging.LogRequests(infoLog)(cgiHandler) + handler := logging.LogRequests(level.Info(baseLog))(cgiHandler) // set up signals to trigger graceful shutdown ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGHUP) defer stop() // run the server - server, err := gemini.NewServer(ctx, "localhost", "tcp4", ":1965", handler, errLog, tlsconf) + server, err := gemini.NewServer(ctx, "localhost", "tcp4", ":1965", handler, baseLog, tlsconf) if err != nil { log.Fatal(err) } diff --git a/examples/cowsay/main.go b/examples/cowsay/main.go index 32ba2ea..3ada714 100644 --- a/examples/cowsay/main.go +++ b/examples/cowsay/main.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" + "github.com/go-kit/log/level" sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/gemini" "tildegit.org/tjp/sliderule/logging" @@ -23,13 +24,13 @@ func main() { log.Fatal(err) } - _, infoLog, _, errLog := logging.DefaultLoggers() + baseLog := logging.Base() // add request logging to the request handler - handler := logging.LogRequests(infoLog)(cowsayHandler) + handler := logging.LogRequests(level.Info(baseLog))(cowsayHandler) // run the server - server, err := gemini.NewServer(context.Background(), "localhost", "tcp4", ":1965", handler, errLog, tlsconf) + server, err := gemini.NewServer(context.Background(), "localhost", "tcp4", ":1965", handler, baseLog, tlsconf) if err != nil { log.Fatal(err) } diff --git a/examples/fileserver/main.go b/examples/fileserver/main.go index e90fdd9..2801858 100644 --- a/examples/fileserver/main.go +++ b/examples/fileserver/main.go @@ -5,6 +5,7 @@ import ( "log" "os" + "github.com/go-kit/log/level" sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/contrib/fs" "tildegit.org/tjp/sliderule/gemini" @@ -35,13 +36,13 @@ func main() { router.Route("/*", handler) handler = router.Handler() - _, infoLog, _, errLog := logging.DefaultLoggers() + baseLog := logging.Base() // add request logging to stdout - handler = logging.LogRequests(infoLog)(handler) + handler = logging.LogRequests(level.Info(baseLog))(handler) // run the server - server, err := gemini.NewServer(context.Background(), "localhost", "tcp4", ":1965", handler, errLog, tlsconf) + server, err := gemini.NewServer(context.Background(), "localhost", "tcp4", ":1965", handler, baseLog, tlsconf) if err != nil { log.Fatal(err) } diff --git a/examples/finger/main.go b/examples/finger/main.go index 723e3e6..8cb6526 100644 --- a/examples/finger/main.go +++ b/examples/finger/main.go @@ -4,20 +4,20 @@ import ( "context" "log" + "github.com/go-kit/log/level" "tildegit.org/tjp/sliderule/finger" "tildegit.org/tjp/sliderule/logging" ) func main() { - _, infoLog, _, errLog := logging.DefaultLoggers() - + baselog := logging.Base() fs, err := finger.NewServer( context.Background(), "localhost", "tcp", ":79", - logging.LogRequests(infoLog)(finger.SystemFinger(false)), - errLog, + logging.LogRequests(level.Info(baselog))(finger.SystemFinger(false)), + logging.Base(), ) if err != nil { log.Fatal(err) diff --git a/examples/gopher_fileserver/main.go b/examples/gopher_fileserver/main.go index 1cb7495..9b8c33f 100644 --- a/examples/gopher_fileserver/main.go +++ b/examples/gopher_fileserver/main.go @@ -4,6 +4,7 @@ import ( "context" "log" + "github.com/go-kit/log/level" sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/contrib/cgi" "tildegit.org/tjp/sliderule/contrib/fs" @@ -19,10 +20,10 @@ func main() { fs.GopherFileHandler(".", "", nil), ) - _, infoLog, _, errLog := logging.DefaultLoggers() - handler = logging.LogRequests(infoLog)(handler) + baseLog := logging.Base() + handler = logging.LogRequests(level.Info(baseLog))(handler) - server, err := gopher.NewServer(context.Background(), "localhost", "tcp4", ":70", handler, errLog) + server, err := gopher.NewServer(context.Background(), "localhost", "tcp4", ":70", handler, baseLog) if err != nil { log.Fatal(err) } diff --git a/examples/inspectls/main.go b/examples/inspectls/main.go index d400fe9..f12fd65 100644 --- a/examples/inspectls/main.go +++ b/examples/inspectls/main.go @@ -12,6 +12,7 @@ import ( "os" "strings" + "github.com/go-kit/log/level" sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/gemini" "tildegit.org/tjp/sliderule/logging" @@ -27,13 +28,13 @@ func main() { log.Fatal(err) } - _, infoLog, _, errLog := logging.DefaultLoggers() + baseLog := logging.Base() // add stdout logging to the request handler - handler := logging.LogRequests(infoLog)(inspectHandler) + handler := logging.LogRequests(level.Info(baseLog))(inspectHandler) // run the server - server, err := gemini.NewServer(context.Background(), "localhost", "tcp4", ":1965", handler, errLog, tlsconf) + server, err := gemini.NewServer(context.Background(), "localhost", "tcp4", ":1965", handler, baseLog, tlsconf) if err != nil { log.Fatal(err) } diff --git a/finger/serve.go b/finger/serve.go index ff842e1..db96088 100644 --- a/finger/serve.go +++ b/finger/serve.go @@ -25,7 +25,7 @@ func NewServer( network string, address string, handler types.Handler, - errLog logging.Logger, + baseLog logging.Logger, ) (types.Server, error) { fs := &fingerServer{handler: handler} @@ -33,7 +33,7 @@ func NewServer( address = internal.JoinDefaultPort(address, "79") var err error - fs.Server, err = internal.NewServer(ctx, hostname, network, address, errLog, fs.handleConn) + fs.Server, err = internal.NewServer(ctx, hostname, network, address, baseLog, fs.handleConn) if err != nil { return nil, err } diff --git a/gemini/serve.go b/gemini/serve.go index 5c6c979..0303c13 100644 --- a/gemini/serve.go +++ b/gemini/serve.go @@ -31,7 +31,7 @@ func NewServer( network string, address string, handler types.Handler, - errorLog logging.Logger, + baseLog logging.Logger, tlsConfig *tls.Config, ) (types.Server, error) { s := &server{handler: handler} @@ -39,7 +39,7 @@ func NewServer( hostname = internal.JoinDefaultPort(hostname, "1965") address = internal.JoinDefaultPort(address, "1965") - internalServer, err := internal.NewServer(ctx, hostname, network, address, errorLog, s.handleConn) + internalServer, err := internal.NewServer(ctx, hostname, network, address, baseLog, s.handleConn) if err != nil { return nil, err } diff --git a/gopher/serve.go b/gopher/serve.go index d78150c..67c889a 100644 --- a/gopher/serve.go +++ b/gopher/serve.go @@ -26,7 +26,7 @@ func NewServer( network string, address string, handler types.Handler, - errLog logging.Logger, + baseLog logging.Logger, ) (types.Server, error) { gs := &gopherServer{handler: handler} @@ -34,7 +34,7 @@ func NewServer( address = internal.JoinDefaultPort(address, "70") var err error - gs.Server, err = internal.NewServer(ctx, hostname, network, address, errLog, gs.handleConn) + gs.Server, err = internal.NewServer(ctx, hostname, network, address, baseLog, gs.handleConn) if err != nil { return nil, err } 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 diff --git a/logging/logger.go b/logging/logger.go index 87b5985..084bea0 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -2,6 +2,7 @@ package logging import ( "os" + "sync" "github.com/go-kit/log" "github.com/go-kit/log/level" @@ -13,31 +14,42 @@ type Logger interface { Log(keyvals ...any) error } -// DefaultLoggers produces helpful base loggers for each level. -// -// They write logfmt to standard out, annotated with ANSI colors depending on the level. -func DefaultLoggers() (debug, info, warn, error Logger) { - base := term.NewLogger(os.Stdout, log.NewLogfmtLogger, func(keyvals ...any) term.FgBgColor { - for i := 0; i < len(keyvals)-1; i += 2 { - if keyvals[i] != "level" { - continue - } +var ( + once sync.Once + baselogger Logger +) - switch keyvals[i+1] { - case level.DebugValue(): - return term.FgBgColor{Fg: term.Gray} - case level.InfoValue(): - return term.FgBgColor{Fg: term.Green} - case level.WarnValue(): - return term.FgBgColor{Fg: term.Yellow} - case level.ErrorValue(): - return term.FgBgColor{Fg: term.Red} +func Base() Logger { + once.Do(func() { + base := term.NewLogger(os.Stdout, log.NewLogfmtLogger, func(keyvals ...any) term.FgBgColor { + for i := 0; i < len(keyvals)-1; i += 2 { + if keyvals[i] != "level" { + continue + } + + switch keyvals[i+1] { + case level.DebugValue(): + return term.FgBgColor{Fg: term.Gray} + case level.InfoValue(): + return term.FgBgColor{Fg: term.Green} + case level.WarnValue(): + return term.FgBgColor{Fg: term.Yellow} + case level.ErrorValue(): + return term.FgBgColor{Fg: term.Red} + } } - } - return term.FgBgColor{} + return term.FgBgColor{} + }) + baselogger = log.NewSyncLogger(base) }) - base = log.NewSyncLogger(base) + return baselogger +} +// DefaultLoggers produces helpful base loggers for each level. +// +// They write logfmt to standard out, annotated with ANSI colors depending on the level. +func DefaultLoggers() (debug, info, warn, error Logger) { + base := Base() return level.Debug(base), level.Info(base), level.Warn(base), level.Error(base) } diff --git a/spartan/serve.go b/spartan/serve.go index 4b37f6a..0fb4939 100644 --- a/spartan/serve.go +++ b/spartan/serve.go @@ -27,7 +27,7 @@ func NewServer( network string, address string, handler types.Handler, - errLog logging.Logger, + baseLog logging.Logger, ) (types.Server, error) { ss := &spartanServer{handler: handler} @@ -35,7 +35,7 @@ func NewServer( address = internal.JoinDefaultPort(address, "300") var err error - ss.Server, err = internal.NewServer(ctx, hostname, network, address, errLog, ss.handleConn) + ss.Server, err = internal.NewServer(ctx, hostname, network, address, baseLog, ss.handleConn) if err != nil { return nil, err } |