From 3e42ccb6f65eebad1e5299232e389b92d3e19b48 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Fri, 25 Aug 2023 10:06:06 -0400 Subject: [PATCH] adding flags for log verbosity, cleaning up refactor --- cmd/echo/main.go | 14 +++++++++++--- logger/wrappedslogger.go | 17 +++++++++++++---- telnet/server.go | 4 ++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/cmd/echo/main.go b/cmd/echo/main.go index 9dcec57..d4494a6 100644 --- a/cmd/echo/main.go +++ b/cmd/echo/main.go @@ -2,9 +2,11 @@ package main import ( "context" + "flag" "fmt" "log" "net" + "os" "github.com/ThreeDotsLabs/watermill" "github.com/ThreeDotsLabs/watermill/message" @@ -17,10 +19,16 @@ import ( ) var ( - port int + port int + verbose int ) func main() { + + flag.IntVar(&verbose, "verbose", 0, "-5:Trace, -4:Debug, 0:Info, 4:Warn, 8:Error") + flag.IntVar(&port, "port", 5555, "MUD server port") + flag.Parse() + l, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) if err != nil { log.Fatalf("error binding to port %d\n: %s", port, err) @@ -28,7 +36,7 @@ func main() { defer l.Close() wmLogger := watermill.NewStdLogger(false, false) - wrappedSlogger := logger.NewWrappedSlogger() + wrappedSlogger := logger.NewWrappedSlogger(os.Stdout, verbose) router, err := message.NewRouter(message.RouterConfig{}, wrappedSlogger) router.AddPlugin(plugin.SignalsHandler) @@ -46,7 +54,7 @@ func main() { go tp.Handle() for { - conn, err := ts.l.Accept() + conn, err := ts.Accept() if err != nil { log.Fatal(err) } diff --git a/logger/wrappedslogger.go b/logger/wrappedslogger.go index 4b14a11..c59909e 100644 --- a/logger/wrappedslogger.go +++ b/logger/wrappedslogger.go @@ -1,11 +1,16 @@ package logger import ( + "io" "log/slog" "github.com/ThreeDotsLabs/watermill" ) +const ( + LevelTrace = slog.Level(-5) +) + func LogFieldsToSlogAttrs(in watermill.LogFields) []slog.Attr { attrs := make([]slog.Attr, 0) for k, v := range in { @@ -21,15 +26,19 @@ type WrappedSlogger struct { *slog.Logger } -func NewWrappedSlogger() *WrappedSlogger { +func NewWrappedSlogger(w io.Writer, verbose int) *WrappedSlogger { + opts := &slog.HandlerOptions{ + Level: slog.Level(verbose), + } + handler := slog.NewTextHandler(w, opts) return &WrappedSlogger{ - slog.Default(), + slog.New(handler), } } func (wl *WrappedSlogger) Error(msg string, err error, fields watermill.LogFields) { attrs := LogFieldsToSlogAttrs(fields) - wl.LogAttrs(nil, slog.Level(-5), msg, attrs...) + wl.LogAttrs(nil, LevelTrace, msg, attrs...) } func (wl *WrappedSlogger) Info(msg string, fields watermill.LogFields) { @@ -44,7 +53,7 @@ func (wl *WrappedSlogger) Debug(msg string, fields watermill.LogFields) { func (wl *WrappedSlogger) Trace(msg string, fields watermill.LogFields) { attrs := LogFieldsToSlogAttrs(fields) - wl.LogAttrs(nil, slog.Level(-5), msg, attrs...) + wl.LogAttrs(nil, LevelTrace, msg, attrs...) } func (wl *WrappedSlogger) With(fields watermill.LogFields) watermill.LoggerAdapter { diff --git a/telnet/server.go b/telnet/server.go index 63a4603..d14fa06 100644 --- a/telnet/server.go +++ b/telnet/server.go @@ -30,6 +30,10 @@ func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher, wml return &ts } +func (ts *TELNETServer) Accept() (net.Conn, error) { + return ts.l.Accept() +} + func (ts *TELNETServer) Handle(conn net.Conn) { s := bufio.NewScanner(conn) defer conn.Close()