From 61bd4b64f5ef159acbe9954a5d0b21c5f8575603 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Fri, 19 Dec 2025 22:26:25 -0600 Subject: [PATCH] setup loggers inside modules with attributes --- internal/module/http-client.go | 5 ++- internal/module/http-server.go | 6 +-- internal/module/interval.go | 5 ++- internal/module/midi-input.go | 27 +++++++------ internal/module/midi-ouptut.go | 29 +++++++------- internal/module/mqtt-client.go | 5 ++- internal/module/nats-client.go | 5 ++- internal/module/psn-client.go | 9 +++-- internal/module/serial-client.go | 63 +++++++++++++++--------------- internal/module/sip-call-server.go | 49 +++++++++++------------ internal/module/sip-dtmf-server.go | 5 ++- internal/module/tcp-client.go | 15 +++---- internal/module/tcp-server.go | 15 +++---- internal/module/timer.go | 5 ++- internal/module/udp-client.go | 5 ++- internal/module/udp-multicast.go | 7 ++-- internal/module/udp-server.go | 7 ++-- internal/processor/debug-log.go | 5 ++- 18 files changed, 142 insertions(+), 125 deletions(-) diff --git a/internal/module/http-client.go b/internal/module/http-client.go index 4e006f4..32f7c9f 100644 --- a/internal/module/http-client.go +++ b/internal/module/http-client.go @@ -16,6 +16,7 @@ type HTTPClient struct { ctx context.Context client *http.Client router route.RouteIO + logger *slog.Logger } func init() { @@ -23,7 +24,7 @@ func init() { Type: "http.client", New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) { - return &HTTPClient{config: config, ctx: ctx, router: router}, nil + return &HTTPClient{config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -43,7 +44,7 @@ func (hc *HTTPClient) Run() error { } <-hc.ctx.Done() - slog.Debug("router context done in module", "id", hc.Id()) + hc.logger.Debug("router context done in module") return nil } diff --git a/internal/module/http-server.go b/internal/module/http-server.go index 7906d74..f16055c 100644 --- a/internal/module/http-server.go +++ b/internal/module/http-server.go @@ -16,6 +16,7 @@ type HTTPServer struct { Port uint16 ctx context.Context router route.RouteIO + logger *slog.Logger } type ResponseData struct { @@ -39,7 +40,7 @@ func init() { return nil, fmt.Errorf("http.server port must be uint16") } - return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router}, nil + return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -89,12 +90,11 @@ func (hs *HTTPServer) Run() error { go func() { <-hs.ctx.Done() - slog.Debug("router context done in module", "id", hs.Id()) + hs.logger.Debug("router context done in module") httpServer.Close() }() err := httpServer.ListenAndServe() - slog.Debug("http.server closed", "id", hs.Id()) // TODO(jwetzell): handle server closed error differently if err != nil { return err diff --git a/internal/module/interval.go b/internal/module/interval.go index 8a09cd0..2ebbf52 100644 --- a/internal/module/interval.go +++ b/internal/module/interval.go @@ -16,6 +16,7 @@ type Interval struct { ctx context.Context router route.RouteIO ticker *time.Ticker + logger *slog.Logger } func init() { @@ -35,7 +36,7 @@ func init() { return nil, fmt.Errorf("gen.interval duration must be number") } - return &Interval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router}, nil + return &Interval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -56,7 +57,7 @@ func (i *Interval) Run() error { for { select { case <-i.ctx.Done(): - slog.Debug("router context done in module", "id", i.Id()) + i.logger.Debug("router context done in module") return nil case <-ticker.C: if i.router != nil { diff --git a/internal/module/midi-input.go b/internal/module/midi-input.go index 74c4a57..0a63d7e 100644 --- a/internal/module/midi-input.go +++ b/internal/module/midi-input.go @@ -19,6 +19,7 @@ type MIDIInput struct { router route.RouteIO Port string SendFunc func(midi.Message) error + logger *slog.Logger } func init() { @@ -38,30 +39,30 @@ func init() { return nil, fmt.Errorf("midi.input port must be a string") } - return &MIDIInput{config: config, Port: portString, ctx: ctx, router: router}, nil + return &MIDIInput{config: config, Port: portString, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } -func (mc *MIDIInput) Id() string { - return mc.config.Id +func (mi *MIDIInput) Id() string { + return mi.config.Id } -func (mc *MIDIInput) Type() string { - return mc.config.Type +func (mi *MIDIInput) Type() string { + return mi.config.Type } -func (mc *MIDIInput) Run() error { +func (mi *MIDIInput) Run() error { defer midi.CloseDriver() - in, err := midi.FindInPort(mc.Port) + in, err := midi.FindInPort(mi.Port) if err != nil { - return fmt.Errorf("midi.input can't find input port: %s", mc.Port) + return fmt.Errorf("midi.input can't find input port: %s", mi.Port) } stop, err := midi.ListenTo(in, func(msg midi.Message, timestampms int32) { - if mc.router != nil { - mc.router.HandleInput(mc.Id(), msg) + if mi.router != nil { + mi.router.HandleInput(mi.Id(), msg) } }, midi.UseSysEx()) @@ -71,11 +72,11 @@ func (mc *MIDIInput) Run() error { defer stop() - <-mc.ctx.Done() - slog.Debug("router context done in module", "id", mc.Id()) + <-mi.ctx.Done() + mi.logger.Debug("router context done in module") return nil } -func (mc *MIDIInput) Output(payload any) error { +func (mi *MIDIInput) Output(payload any) error { return fmt.Errorf("midi.input output is not implemented") } diff --git a/internal/module/midi-ouptut.go b/internal/module/midi-ouptut.go index b26c4fd..dc78529 100644 --- a/internal/module/midi-ouptut.go +++ b/internal/module/midi-ouptut.go @@ -19,6 +19,7 @@ type MIDIOutput struct { router route.RouteIO Port string SendFunc func(midi.Message) error + logger *slog.Logger } func init() { @@ -39,26 +40,26 @@ func init() { return nil, fmt.Errorf("midi.output port must be a string") } - return &MIDIOutput{config: config, Port: portString, ctx: ctx, router: router}, nil + return &MIDIOutput{config: config, Port: portString, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } -func (mc *MIDIOutput) Id() string { - return mc.config.Id +func (mo *MIDIOutput) Id() string { + return mo.config.Id } -func (mc *MIDIOutput) Type() string { - return mc.config.Type +func (mo *MIDIOutput) Type() string { + return mo.config.Type } -func (mc *MIDIOutput) Run() error { +func (mo *MIDIOutput) Run() error { defer midi.CloseDriver() - out, err := midi.FindOutPort(mc.Port) + out, err := midi.FindOutPort(mo.Port) if err != nil { - return fmt.Errorf("midi.output can't find output port: %s", mc.Port) + return fmt.Errorf("midi.output can't find output port: %s", mo.Port) } send, err := midi.SendTo(out) @@ -66,15 +67,15 @@ func (mc *MIDIOutput) Run() error { return err } - mc.SendFunc = send + mo.SendFunc = send - <-mc.ctx.Done() - slog.Debug("router context done in module", "id", mc.Id()) + <-mo.ctx.Done() + mo.logger.Debug("router context done in module") return nil } -func (mc *MIDIOutput) Output(payload any) error { - if mc.SendFunc == nil { +func (mo *MIDIOutput) Output(payload any) error { + if mo.SendFunc == nil { return fmt.Errorf("midi.output output is not setup") } @@ -84,5 +85,5 @@ func (mc *MIDIOutput) Output(payload any) error { return fmt.Errorf("midi.output can only ouptut midi.Message") } - return mc.SendFunc(payloadMessage) + return mo.SendFunc(payloadMessage) } diff --git a/internal/module/mqtt-client.go b/internal/module/mqtt-client.go index 5bb8154..6970fd9 100644 --- a/internal/module/mqtt-client.go +++ b/internal/module/mqtt-client.go @@ -18,6 +18,7 @@ type MQTTClient struct { ClientID string Topic string client mqtt.Client + logger *slog.Logger } func init() { @@ -61,7 +62,7 @@ func init() { return nil, fmt.Errorf("mqtt.client clientId must be string") } - return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router}, nil + return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -99,7 +100,7 @@ func (mc *MQTTClient) Run() error { } <-mc.ctx.Done() - slog.Debug("router context done in module", "id", mc.Id()) + mc.logger.Debug("router context done in module") return nil } diff --git a/internal/module/nats-client.go b/internal/module/nats-client.go index 4390a34..4416f1f 100644 --- a/internal/module/nats-client.go +++ b/internal/module/nats-client.go @@ -18,6 +18,7 @@ type NATSClient struct { URL string Subject string client *nats.Conn + logger *slog.Logger } func init() { @@ -49,7 +50,7 @@ func init() { return nil, fmt.Errorf("nats.client subject must be string") } - return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router}, nil + return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -87,7 +88,7 @@ func (nc *NATSClient) Run() error { defer sub.Unsubscribe() <-nc.ctx.Done() - slog.Debug("router context done in module", "id", nc.Id()) + nc.logger.Debug("router context done in module") return nil } diff --git a/internal/module/psn-client.go b/internal/module/psn-client.go index 85d2775..60f6f62 100644 --- a/internal/module/psn-client.go +++ b/internal/module/psn-client.go @@ -18,6 +18,7 @@ type PSNClient struct { ctx context.Context router route.RouteIO decoder *psn.Decoder + logger *slog.Logger } func init() { @@ -25,7 +26,7 @@ func init() { Type: "psn.client", New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) { - return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router}, nil + return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -58,7 +59,7 @@ func (pc *PSNClient) Run() error { select { case <-pc.ctx.Done(): // TODO(jwetzell): cleanup? - slog.Debug("router context done in module", "id", pc.Id()) + pc.logger.Debug("router context done in module") return nil default: pc.conn.SetDeadline(time.Now().Add(time.Millisecond * 200)) @@ -76,7 +77,7 @@ func (pc *PSNClient) Run() error { message := buffer[:numBytes] err := pc.decoder.Decode(message) if err != nil { - slog.Error("psn.client problem decoding psn traffic", "id", pc.Id(), "error", err) + pc.logger.Error("psn.client problem decoding psn traffic", "error", err) } if pc.router != nil { @@ -84,7 +85,7 @@ func (pc *PSNClient) Run() error { pc.router.HandleInput(pc.Id(), tracker) } } else { - slog.Error("psn.client has no router", "id", pc.Id()) + pc.logger.Error("psn.client has no router") } } } diff --git a/internal/module/serial-client.go b/internal/module/serial-client.go index 238bce4..1b2c337 100644 --- a/internal/module/serial-client.go +++ b/internal/module/serial-client.go @@ -22,6 +22,7 @@ type SerialClient struct { Framer framer.Framer Mode *serial.Mode port serial.Port + logger *slog.Logger } func init() { @@ -74,82 +75,82 @@ func init() { BaudRate: int(baudRateNum), } - return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, ctx: ctx, router: router}, nil + return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } -func (mc *SerialClient) Id() string { - return mc.config.Id +func (sc *SerialClient) Id() string { + return sc.config.Id } -func (mc *SerialClient) Type() string { - return mc.config.Type +func (sc *SerialClient) Type() string { + return sc.config.Type } -func (mc *SerialClient) SetupPort() error { +func (sc *SerialClient) SetupPort() error { - port, err := serial.Open(mc.Port, mc.Mode) + port, err := serial.Open(sc.Port, sc.Mode) if err != nil { - return fmt.Errorf("serial.client can't open input port: %s", mc.Port) + return fmt.Errorf("serial.client can't open input port: %s", sc.Port) } - mc.port = port + sc.port = port return nil } -func (mc *SerialClient) Run() error { +func (sc *SerialClient) Run() error { // TODO(jwetzell): shutdown with router.Context properly go func() { - <-mc.ctx.Done() - slog.Debug("router context done in module", "id", mc.Id()) - if mc.port != nil { - mc.port.Close() + <-sc.ctx.Done() + sc.logger.Debug("router context done in module") + if sc.port != nil { + sc.port.Close() } }() for { - err := mc.SetupPort() + err := sc.SetupPort() if err != nil { - if mc.ctx.Err() != nil { - slog.Debug("router context done in module", "id", mc.Id()) + if sc.ctx.Err() != nil { + sc.logger.Debug("router context done in module") return nil } - slog.Error("serial.client", "id", mc.Id(), "error", err.Error()) + sc.logger.Error("serial.client", "error", err.Error()) time.Sleep(time.Second * 2) continue } buffer := make([]byte, 1024) select { - case <-mc.ctx.Done(): - slog.Debug("router context done in module", "id", mc.Id()) + case <-sc.ctx.Done(): + sc.logger.Debug("router context done in module") return nil default: READ: for { select { - case <-mc.ctx.Done(): - slog.Debug("router context done in module", "id", mc.Id()) + case <-sc.ctx.Done(): + sc.logger.Debug("router context done in module") return nil default: - byteCount, err := mc.port.Read(buffer) + byteCount, err := sc.port.Read(buffer) if err != nil { - mc.Framer.Clear() + sc.Framer.Clear() break READ } - if mc.Framer != nil { + if sc.Framer != nil { if byteCount > 0 { - messages := mc.Framer.Decode(buffer[0:byteCount]) + messages := sc.Framer.Decode(buffer[0:byteCount]) for _, message := range messages { - if mc.router != nil { - mc.router.HandleInput(mc.Id(), message) + if sc.router != nil { + sc.router.HandleInput(sc.Id(), message) } else { - slog.Error("serial.client has no router", "id", mc.Id()) + sc.logger.Error("serial.client has no router") } } } @@ -160,7 +161,7 @@ func (mc *SerialClient) Run() error { } } -func (mc *SerialClient) Output(payload any) error { +func (sc *SerialClient) Output(payload any) error { payloadBytes, ok := payload.([]byte) @@ -168,6 +169,6 @@ func (mc *SerialClient) Output(payload any) error { return fmt.Errorf("serial.client can only ouptut bytes") } - _, err := mc.port.Write(mc.Framer.Encode(payloadBytes)) + _, err := sc.port.Write(sc.Framer.Encode(payloadBytes)) return err } diff --git a/internal/module/sip-call-server.go b/internal/module/sip-call-server.go index 9f1e0d4..9f9181b 100644 --- a/internal/module/sip-call-server.go +++ b/internal/module/sip-call-server.go @@ -24,6 +24,7 @@ type SIPCallServer struct { Transport string UserAgent string dg *diago.Diago + logger *slog.Logger } type SIPCallMessage struct { @@ -85,24 +86,24 @@ func init() { } userAgentString = specificTransportString } - return &SIPCallServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString}, nil + return &SIPCallServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } -func (sds *SIPCallServer) Id() string { - return sds.config.Id +func (scs *SIPCallServer) Id() string { + return scs.config.Id } -func (sds *SIPCallServer) Type() string { - return sds.config.Type +func (scs *SIPCallServer) Type() string { + return scs.config.Type } -func (sds *SIPCallServer) Run() error { +func (scs *SIPCallServer) Run() error { diagoLogger := slog.New(slog.NewJSONHandler(io.Discard, nil)) ua, _ := sipgo.NewUA( - sipgo.WithUserAgent(sds.UserAgent), + sipgo.WithUserAgent(scs.UserAgent), sipgo.WithUserAgentTransportLayerOptions(sip.WithTransportLayerLogger(diagoLogger)), sipgo.WithUserAgentTransactionLayerOptions(sip.WithTransactionLayerLogger(diagoLogger)), ) @@ -112,43 +113,43 @@ func (sds *SIPCallServer) Run() error { media.SetDefaultLogger(diagoLogger) dg := diago.NewDiago(ua, diago.WithLogger(diagoLogger), diago.WithTransport( diago.Transport{ - Transport: sds.Transport, - BindHost: sds.IP, - BindPort: sds.Port, + Transport: scs.Transport, + BindHost: scs.IP, + BindPort: scs.Port, }, )) go func() { - dg.Serve(sds.ctx, func(inDialog *diago.DialogServerSession) { - sds.HandleCall(inDialog) + dg.Serve(scs.ctx, func(inDialog *diago.DialogServerSession) { + scs.HandleCall(inDialog) }) }() - sds.dg = dg + scs.dg = dg - <-sds.ctx.Done() - slog.Debug("router context done in module", "id", sds.Id()) + <-scs.ctx.Done() + scs.logger.Debug("router context done in module") return nil } -func (sds *SIPCallServer) HandleCall(inDialog *diago.DialogServerSession) { +func (scs *SIPCallServer) HandleCall(inDialog *diago.DialogServerSession) { inDialog.Trying() inDialog.Ringing() inDialog.Answer() - sds.router.HandleInput(sds.Id(), SIPCallMessage{ + scs.router.HandleInput(scs.Id(), SIPCallMessage{ To: inDialog.ToUser(), }) <-inDialog.Context().Done() } -func (sds *SIPCallServer) Output(payload any) error { +func (scs *SIPCallServer) Output(payload any) error { payloadMsg, ok := payload.(string) if !ok { return fmt.Errorf("sip.call.server output payload must be of type string") } - if sds.dg == nil { + if scs.dg == nil { return fmt.Errorf("sip.call.server diago is not initialized") } @@ -157,27 +158,27 @@ func (sds *SIPCallServer) Output(payload any) error { if err != nil { return fmt.Errorf("sip.call.server output payload is not a valid SIP URI: %v", err) } - outDialog, err := sds.dg.NewDialog(uri, diago.NewDialogOptions{ - Transport: sds.Transport, + outDialog, err := scs.dg.NewDialog(uri, diago.NewDialogOptions{ + Transport: scs.Transport, }) if err != nil { return fmt.Errorf("sip.call.server failed to create new dialog: %v", err) } - err = outDialog.Invite(sds.ctx, diago.InviteClientOptions{}) + err = outDialog.Invite(scs.ctx, diago.InviteClientOptions{}) if err != nil { return fmt.Errorf("sip.call.server failed to send invite: %v", err) } - err = outDialog.Ack(sds.ctx) + err = outDialog.Ack(scs.ctx) if err != nil { return fmt.Errorf("sip.call.server failed to send ack: %v", err) } // TODO(jwetzell): make this configurable // NOTE(jwetzell): wait 5 seconds before hanging up the call time.Sleep(5 * time.Second) - err = outDialog.Hangup(sds.ctx) + err = outDialog.Hangup(scs.ctx) if err != nil { return fmt.Errorf("sip.call.server failed to hangup call: %v", err) } diff --git a/internal/module/sip-dtmf-server.go b/internal/module/sip-dtmf-server.go index 9225a13..d84db96 100644 --- a/internal/module/sip-dtmf-server.go +++ b/internal/module/sip-dtmf-server.go @@ -24,6 +24,7 @@ type SIPDTMFServer struct { Port int Transport string Separator string + logger *slog.Logger } type SIPDTMFMessage struct { @@ -90,7 +91,7 @@ func init() { if !strings.ContainsRune("0123456789*#ABCD", rune(separatorString[0])) { return nil, fmt.Errorf("sip.dtmf.server separator must be a valid DTMF character") } - return &SIPDTMFServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, Separator: separatorString}, nil + return &SIPDTMFServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, Separator: separatorString, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -131,7 +132,7 @@ func (sds *SIPDTMFServer) Run() error { } <-sds.ctx.Done() - slog.Debug("router context done in module", "id", sds.Id()) + sds.logger.Debug("router context done in module") return nil } diff --git a/internal/module/tcp-client.go b/internal/module/tcp-client.go index fe163c1..91dd702 100644 --- a/internal/module/tcp-client.go +++ b/internal/module/tcp-client.go @@ -19,6 +19,7 @@ type TCPClient struct { ctx context.Context router route.RouteIO Addr *net.TCPAddr + logger *slog.Logger } func init() { @@ -73,7 +74,7 @@ func init() { return nil, err } - return &TCPClient{framer: framer, Addr: addr, config: config, ctx: ctx, router: router}, nil + return &TCPClient{framer: framer, Addr: addr, config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -91,7 +92,7 @@ func (tc *TCPClient) Run() error { // TODO(jwetzell): shutdown with router.Context properly go func() { <-tc.ctx.Done() - slog.Debug("router context done in module", "id", tc.Id()) + tc.logger.Debug("router context done in module") if tc.conn != nil { tc.conn.Close() } @@ -101,10 +102,10 @@ func (tc *TCPClient) Run() error { err := tc.SetupConn() if err != nil { if tc.ctx.Err() != nil { - slog.Debug("router context done in module", "id", tc.Id()) + tc.logger.Debug("router context done in module") return nil } - slog.Error("net.tcp.client", "id", tc.Id(), "error", err.Error()) + tc.logger.Error("net.tcp.client", "error", err.Error()) time.Sleep(time.Second * 2) continue } @@ -112,14 +113,14 @@ func (tc *TCPClient) Run() error { buffer := make([]byte, 1024) select { case <-tc.ctx.Done(): - slog.Debug("router context done in module", "id", tc.Id()) + tc.logger.Debug("router context done in module") return nil default: READ: for { select { case <-tc.ctx.Done(): - slog.Debug("router context done in module", "id", tc.Id()) + tc.logger.Debug("router context done in module") return nil default: byteCount, err := tc.conn.Read(buffer) @@ -136,7 +137,7 @@ func (tc *TCPClient) Run() error { if tc.router != nil { tc.router.HandleInput(tc.Id(), message) } else { - slog.Error("net.tcp.client has no router", "id", tc.Id()) + tc.logger.Error("net.tcp.client has no router") } } } diff --git a/internal/module/tcp-server.go b/internal/module/tcp-server.go index b4feaf5..c782791 100644 --- a/internal/module/tcp-server.go +++ b/internal/module/tcp-server.go @@ -26,6 +26,7 @@ type TCPServer struct { wg sync.WaitGroup connections []*net.TCPConn connectionsMu sync.RWMutex + logger *slog.Logger } func init() { @@ -81,7 +82,7 @@ func init() { return nil, err } - return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), ctx: ctx, router: router}, nil + return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -98,7 +99,7 @@ func (ts *TCPServer) handleClient(client *net.TCPConn) { ts.connectionsMu.Lock() ts.connections = append(ts.connections, client) ts.connectionsMu.Unlock() - slog.Debug("net.tcp.server connection accepted", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String()) + ts.logger.Debug("net.tcp.server connection accepted", "remoteAddr", client.RemoteAddr().String()) defer client.Close() buffer := make([]byte, 1024) @@ -125,7 +126,7 @@ ClientRead: break } } - slog.Debug("net.tcp.server connection reset", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String()) + ts.logger.Debug("net.tcp.server connection reset", "remoteAddr", client.RemoteAddr().String()) ts.connectionsMu.Unlock() } } @@ -138,7 +139,7 @@ ClientRead: break } } - slog.Debug("net.tcp.server stream ended", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String()) + ts.logger.Debug("net.tcp.server stream ended", "remoteAddr", client.RemoteAddr().String()) ts.connectionsMu.Unlock() } return @@ -150,7 +151,7 @@ ClientRead: if ts.router != nil { ts.router.HandleInput(ts.Id(), message) } else { - slog.Error("net.tcp.server has no router", "id", ts.Id()) + ts.logger.Error("net.tcp.server has no router") } } } @@ -170,7 +171,7 @@ func (ts *TCPServer) Run() error { <-ts.ctx.Done() close(ts.quit) listener.Close() - slog.Debug("router context done in module", "id", ts.Id()) + ts.logger.Debug("router context done in module") }() AcceptLoop: @@ -181,7 +182,7 @@ AcceptLoop: case <-ts.quit: break AcceptLoop default: - slog.Debug("net.tcp.server problem with listener", "error", err) + ts.logger.Debug("net.tcp.server problem with listener", "error", err) } } else { ts.wg.Add(1) diff --git a/internal/module/timer.go b/internal/module/timer.go index bc800bf..7526e58 100644 --- a/internal/module/timer.go +++ b/internal/module/timer.go @@ -16,6 +16,7 @@ type Timer struct { ctx context.Context router route.RouteIO timer *time.Timer + logger *slog.Logger } func init() { @@ -35,7 +36,7 @@ func init() { return nil, fmt.Errorf("gen.timer duration must be a number") } - return &Timer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router}, nil + return &Timer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -55,7 +56,7 @@ func (t *Timer) Run() error { select { case <-t.ctx.Done(): t.timer.Stop() - slog.Debug("router context done in module", "id", t.Id()) + t.logger.Debug("router context done in module") return nil case time := <-t.timer.C: if t.router != nil { diff --git a/internal/module/udp-client.go b/internal/module/udp-client.go index c488a78..7be0765 100644 --- a/internal/module/udp-client.go +++ b/internal/module/udp-client.go @@ -17,6 +17,7 @@ type UDPClient struct { conn *net.UDPConn ctx context.Context router route.RouteIO + logger *slog.Logger } func init() { @@ -52,7 +53,7 @@ func init() { return nil, err } - return &UDPClient{Addr: addr, config: config, ctx: ctx, router: router}, nil + return &UDPClient{Addr: addr, config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -79,7 +80,7 @@ func (uc *UDPClient) Run() error { } <-uc.ctx.Done() - slog.Debug("router context done in module", "id", uc.Id()) + uc.logger.Debug("router context done in module") if uc.conn != nil { uc.conn.Close() } diff --git a/internal/module/udp-multicast.go b/internal/module/udp-multicast.go index 8878db0..2eb9d67 100644 --- a/internal/module/udp-multicast.go +++ b/internal/module/udp-multicast.go @@ -17,6 +17,7 @@ type UDPMulticast struct { ctx context.Context router route.RouteIO Addr *net.UDPAddr + logger *slog.Logger } func init() { @@ -51,7 +52,7 @@ func init() { if err != nil { return nil, err } - return &UDPMulticast{config: config, Addr: addr, ctx: ctx, router: router}, nil + return &UDPMulticast{config: config, Addr: addr, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -79,7 +80,7 @@ func (um *UDPMulticast) Run() error { select { case <-um.ctx.Done(): // TODO(jwetzell): cleanup? - slog.Debug("router context done in module", "id", um.Id()) + um.logger.Debug("router context done in module") return nil default: um.conn.SetDeadline(time.Now().Add(time.Millisecond * 200)) @@ -99,7 +100,7 @@ func (um *UDPMulticast) Run() error { if um.router != nil { um.router.HandleInput(um.Id(), message) } else { - slog.Error("net.udp.multicast has no router", "id", um.Id()) + um.logger.Error("net.udp.multicast has no router") } } } diff --git a/internal/module/udp-server.go b/internal/module/udp-server.go index 17aba95..67d2e59 100644 --- a/internal/module/udp-server.go +++ b/internal/module/udp-server.go @@ -17,6 +17,7 @@ type UDPServer struct { config config.ModuleConfig ctx context.Context router route.RouteIO + logger *slog.Logger } func init() { @@ -53,7 +54,7 @@ func init() { log.Fatalf("error resolving UDP address: %v", err) } - return &UDPServer{Addr: addr, config: config, ctx: ctx, router: router}, nil + return &UDPServer{Addr: addr, config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil }, }) } @@ -81,7 +82,7 @@ func (us *UDPServer) Run() error { select { case <-us.ctx.Done(): // TODO(jwetzell): cleanup? - slog.Debug("router context done in module", "id", us.Id()) + us.logger.Debug("router context done in module") return nil default: listener.SetDeadline(time.Now().Add(time.Millisecond * 200)) @@ -98,7 +99,7 @@ func (us *UDPServer) Run() error { if us.router != nil { us.router.HandleInput(us.Id(), message) } else { - slog.Error("net.udp.server has no router", "id", us.Id()) + us.logger.Error("net.udp.server has no router") } } } diff --git a/internal/processor/debug-log.go b/internal/processor/debug-log.go index 125ebec..aa03f40 100644 --- a/internal/processor/debug-log.go +++ b/internal/processor/debug-log.go @@ -10,10 +10,11 @@ import ( type DebugLog struct { config config.ProcessorConfig + logger *slog.Logger } func (dl *DebugLog) Process(ctx context.Context, payload any) (any, error) { - slog.Debug("debug.log", "payload", payload, "payloadType", fmt.Sprintf("%T", payload)) + dl.logger.Debug("debug.log", "payload", payload, "payloadType", fmt.Sprintf("%T", payload)) return payload, nil } @@ -25,7 +26,7 @@ func init() { RegisterProcessor(ProcessorRegistration{ Type: "debug.log", New: func(config config.ProcessorConfig) (Processor, error) { - return &DebugLog{config: config}, nil + return &DebugLog{config: config, logger: slog.Default().With("component", "processor")}, nil }, }) }