From e3e46ae2bd6f5b87ae8ecc0ce516d6235e973c42 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Fri, 25 Aug 2023 11:52:46 -0400 Subject: [PATCH] cleaning up log handling with closures --- cmd/echo/main.go | 2 +- telnet/server.go | 36 +++++++++++++++++++----------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cmd/echo/main.go b/cmd/echo/main.go index 72eb3ac..71c716d 100644 --- a/cmd/echo/main.go +++ b/cmd/echo/main.go @@ -82,6 +82,6 @@ func main() { log.Fatal(err) } - go ts.Handle(conn) + go ts.Handle(wrappedSlogger)(conn) } } diff --git a/telnet/server.go b/telnet/server.go index 131832b..d9be755 100644 --- a/telnet/server.go +++ b/telnet/server.go @@ -43,27 +43,29 @@ func (ts *TELNETServer) Accept() (net.Conn, error) { return conn, err } -func (ts *TELNETServer) Handle(conn net.Conn) { - s := bufio.NewScanner(conn) - defer conn.Close() - // i need to create an ecs Renderer that targets a iw.Writer (net.Conn) - // the renderer - - correlationID := watermill.NewUUID() - ts.logger = ts.logger.With( +func (ts *TELNETServer) Handle(wml watermill.LoggerAdapter) func(conn net.Conn) { + logger := wml.With( watermill.LogFields{ - "correlation_id": correlationID, + // "correlation_id": correlationID, }, ) + return func(conn net.Conn) { + s := bufio.NewScanner(conn) + defer conn.Close() + // i need to create an ecs Renderer that targets a iw.Writer (net.Conn) + // the renderer + + correlationID := watermill.NewUUID() - for s.Scan() { - t := s.Text() - m := message.NewMessage(watermill.NewUUID(), []byte(t)) - middleware.SetCorrelationID(correlationID, m) - err := ts.p.Publish("telnet.raw", m) - if err != nil { - log.Fatalln("couldn't write to telnet.raw") + for s.Scan() { + t := s.Text() + m := message.NewMessage(watermill.NewUUID(), []byte(t)) + middleware.SetCorrelationID(correlationID, m) + err := ts.p.Publish("telnet.raw", m) + if err != nil { + log.Fatalln("couldn't write to telnet.raw") + } + logger.Trace("received bytes over telnet", nil) } - ts.logger.Trace("received bytes over telnet", nil) } }