main
Nick Dumas 1 year ago
parent 53739fbc1d
commit 13fdf6160c

@ -0,0 +1,55 @@
package main
import (
"log/slog"
"github.com/ThreeDotsLabs/watermill"
)
func LogFieldsToSlogAttrs(in watermill.LogFields) []slog.Attr {
attrs := make([]slog.Attr, 0)
for k, v := range in {
attrs = append(attrs, slog.Attr{
Key: k,
Value: slog.AnyValue(v),
})
}
return attrs
}
type WrappedSlogger struct {
*slog.Logger
}
func NewWrappedSlogger() *WrappedSlogger {
return &WrappedSlogger{
slog.Default(),
}
}
func (wl *WrappedSlogger) Error(msg string, err error, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields)
wl.LogAttrs(nil, slog.Level(-5), msg, attrs...)
}
func (wl *WrappedSlogger) Info(msg string, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields)
wl.LogAttrs(nil, slog.LevelInfo, msg, attrs...)
}
func (wl *WrappedSlogger) Debug(msg string, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields)
wl.LogAttrs(nil, slog.LevelDebug, msg, attrs...)
}
func (wl *WrappedSlogger) Trace(msg string, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields)
wl.LogAttrs(nil, slog.Level(-5), msg, attrs...)
}
func (wl *WrappedSlogger) With(fields watermill.LogFields) watermill.LoggerAdapter {
attrs := LogFieldsToSlogAttrs(fields)
l := slog.Default().With(attrs)
return &WrappedSlogger{l}
}

@ -19,7 +19,9 @@ func main() {
} }
defer l.Close() defer l.Close()
wmLogger := watermill.NewStdLogger(false, false) wmLogger := watermill.NewStdLogger(false, false)
router, err := message.NewRouter(message.RouterConfig{}, wmLogger)
logger := NewWrappedSlogger()
router, err := message.NewRouter(message.RouterConfig{}, logger)
router.AddPlugin(plugin.SignalsHandler) router.AddPlugin(plugin.SignalsHandler)
@ -31,8 +33,8 @@ func main() {
pubSub := gochannel.NewGoChannel(gochannel.Config{}, wmLogger) pubSub := gochannel.NewGoChannel(gochannel.Config{}, wmLogger)
ctx := context.Background() ctx := context.Background()
ts := NewTELNETServer(ctx, l, pubSub) ts := NewTELNETServer(ctx, l, pubSub, logger)
tp := NewTELNETParser(ctx, pubSub) tp := NewTELNETParser(ctx, pubSub, logger)
go tp.Handle() go tp.Handle()
for { for {

@ -8,11 +8,13 @@ import (
"github.com/ThreeDotsLabs/watermill" "github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message" "github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
) )
type TELNETParser struct { type TELNETParser struct {
s message.Subscriber s message.Subscriber
c context.Context c context.Context
logger watermill.LoggerAdapter
} }
func (tp *TELNETParser) Handle() { func (tp *TELNETParser) Handle() {
@ -22,16 +24,20 @@ func (tp *TELNETParser) Handle() {
} }
for message := range messages { for message := range messages {
log.Printf("%#+v\n", message) fields := make(watermill.LogFields)
fields["body"] = string(message.Payload)
fields["correlation_id"] = string(message.Metadata["correlation_id"])
tp.logger.Info("received raw TCP line", fields)
message.Ack() message.Ack()
} }
} }
func NewTELNETParser(c context.Context, s message.Subscriber) *TELNETParser { func NewTELNETParser(c context.Context, s message.Subscriber, wml watermill.LoggerAdapter) *TELNETParser {
return &TELNETParser{ return &TELNETParser{
c: c, c: c,
s: s, s: s,
logger: wml,
} }
} }
@ -39,13 +45,15 @@ type TELNETServer struct {
l net.Listener l net.Listener
p message.Publisher p message.Publisher
c context.Context c context.Context
logger watermill.LoggerAdapter
} }
func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher) *TELNETServer { func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher, wml watermill.LoggerAdapter) *TELNETServer {
ts := TELNETServer{ ts := TELNETServer{
c: c, c: c,
l: l, l: l,
p: p, p: p,
logger: wml,
} }
return &ts return &ts
@ -55,12 +63,20 @@ func (ts *TELNETServer) Handle(conn net.Conn) {
s := bufio.NewScanner(conn) s := bufio.NewScanner(conn)
defer conn.Close() defer conn.Close()
correlationID := watermill.NewUUID()
ts.logger = ts.logger.With(
watermill.LogFields{
"correlation_id": correlationID},
)
for s.Scan() { for s.Scan() {
t := s.Text() t := s.Text()
err := ts.p.Publish("telnet.raw", message.NewMessage(watermill.NewUUID(), []byte(t))) m := message.NewMessage(watermill.NewUUID(), []byte(t))
middleware.SetCorrelationID(correlationID, m)
err := ts.p.Publish("telnet.raw", m)
if err != nil { if err != nil {
log.Fatalln("couldn't write to telnet.raw") log.Fatalln("couldn't write to telnet.raw")
} }
log.Printf("Received user input: %q\n", t) ts.logger.Info("received bytes over telnet", nil)
} }
} }

Loading…
Cancel
Save