Compare commits

..

No commits in common. 'dev' and 'main' have entirely different histories.
dev ... main

3
.gitignore vendored

@ -2,6 +2,7 @@ node_modules/*
package*.json package*.json
dist/* dist/*
reports/* reports/*
*.log lexer.log
parser.log
bazel-* bazel-*
*.bazel.lock *.bazel.lock

@ -36,28 +36,7 @@ func main() {
defer l.Close() defer l.Close()
wmLogger := watermill.NewStdLogger(false, false) wmLogger := watermill.NewStdLogger(false, false)
traceFile, err := os.OpenFile("trace.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) wrappedSlogger := logger.NewWrappedSlogger(os.Stdout, verbose)
if err != nil {
log.Fatalf("couldn't open trace log: %s\n", err)
}
traceLogConfig := logger.LogConfig{
Writer: traceFile,
Format: "json",
Level: -5,
}
stdoutLogConfig := logger.LogConfig{
Writer: os.Stdout,
Format: "text",
Level: verbose,
}
wrappedSlogger := logger.NewWrappedSlogger(
traceLogConfig,
stdoutLogConfig,
)
router, err := message.NewRouter(message.RouterConfig{}, wrappedSlogger) router, err := message.NewRouter(message.RouterConfig{}, wrappedSlogger)
router.AddPlugin(plugin.SignalsHandler) router.AddPlugin(plugin.SignalsHandler)
@ -72,8 +51,6 @@ func main() {
ctx := context.Background() ctx := context.Background()
ts := telnet.NewTELNETServer(ctx, l, pubSub, wrappedSlogger) ts := telnet.NewTELNETServer(ctx, l, pubSub, wrappedSlogger)
tp := telnet.NewTELNETParser(ctx, pubSub, wrappedSlogger) tp := telnet.NewTELNETParser(ctx, pubSub, wrappedSlogger)
wrappedSlogger.Info("booting up, welcome back formers", nil)
go tp.Handle() go tp.Handle()
for { for {

@ -23,80 +23,42 @@ func LogFieldsToSlogAttrs(in watermill.LogFields) []slog.Attr {
} }
type WrappedSlogger struct { type WrappedSlogger struct {
loggers []*slog.Logger *slog.Logger
} }
type LogConfig struct { func NewWrappedSlogger(w io.Writer, verbose int) *WrappedSlogger {
Format string opts := &slog.HandlerOptions{
Writer io.Writer Level: slog.Level(verbose),
Level int
}
func NewWrappedSlogger(configs ...LogConfig) *WrappedSlogger {
loggers := make([]*slog.Logger, 0)
for _, config := range configs {
opts := &slog.HandlerOptions{
Level: slog.Level(config.Level),
}
var handler slog.Handler
switch config.Format {
case "json":
handler = slog.NewJSONHandler(config.Writer, opts)
case "text":
handler = slog.NewTextHandler(config.Writer, opts)
}
loggers = append(loggers, slog.New(handler))
} }
handler := slog.NewTextHandler(w, opts)
return &WrappedSlogger{ return &WrappedSlogger{
loggers: loggers, slog.New(handler),
} }
} }
func (wl *WrappedSlogger) Error(msg string, err error, fields watermill.LogFields) { func (wl *WrappedSlogger) Error(msg string, err error, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields) attrs := LogFieldsToSlogAttrs(fields)
for _, l := range wl.loggers { wl.LogAttrs(nil, LevelTrace, msg, attrs...)
l.LogAttrs(nil, slog.LevelError, msg, attrs...)
}
} }
func (wl *WrappedSlogger) Info(msg string, fields watermill.LogFields) { func (wl *WrappedSlogger) Info(msg string, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields) attrs := LogFieldsToSlogAttrs(fields)
for _, l := range wl.loggers { wl.LogAttrs(nil, slog.LevelInfo, msg, attrs...)
l.LogAttrs(nil, slog.LevelInfo, msg, attrs...)
}
} }
func (wl *WrappedSlogger) Debug(msg string, fields watermill.LogFields) { func (wl *WrappedSlogger) Debug(msg string, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields) attrs := LogFieldsToSlogAttrs(fields)
for _, l := range wl.loggers { wl.LogAttrs(nil, slog.LevelDebug, msg, attrs...)
l.LogAttrs(nil, slog.LevelDebug, msg, attrs...)
}
} }
func (wl *WrappedSlogger) Trace(msg string, fields watermill.LogFields) { func (wl *WrappedSlogger) Trace(msg string, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields) attrs := LogFieldsToSlogAttrs(fields)
for _, l := range wl.loggers { wl.LogAttrs(nil, LevelTrace, msg, attrs...)
l.LogAttrs(nil, LevelTrace, msg, attrs...)
}
} }
func (wl *WrappedSlogger) With(fields watermill.LogFields) watermill.LoggerAdapter { func (wl *WrappedSlogger) With(fields watermill.LogFields) watermill.LoggerAdapter {
attrs := LogFieldsToSlogAttrs(fields) attrs := LogFieldsToSlogAttrs(fields)
loggerArgs := make([]any, len(attrs)) l := slog.Default().With(attrs)
for idx, attr := range attrs { return &WrappedSlogger{l}
loggerArgs[idx] = attr
}
newLoggers := make([]*slog.Logger, 0)
for _, l := range wl.loggers {
newLoggers = append(newLoggers, l.With(loggerArgs...))
}
return &WrappedSlogger{loggers: newLoggers}
} }

@ -33,12 +33,8 @@ func (tp *TELNETParser) Handle() {
func NewTELNETParser(c context.Context, s message.Subscriber, wml watermill.LoggerAdapter) *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.With( logger: wml,
watermill.LogFields{
"name": "TELNETParser",
},
),
} }
} }

@ -21,26 +21,17 @@ type TELNETServer struct {
func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher, wml watermill.LoggerAdapter) *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.With( logger: wml,
watermill.LogFields{
"name": "TELNETServer",
},
),
} }
return &ts return &ts
} }
func (ts *TELNETServer) Accept() (net.Conn, error) { func (ts *TELNETServer) Accept() (net.Conn, error) {
conn, err := ts.l.Accept() return ts.l.Accept()
ts.logger.Trace("receiving telnet connection", watermill.LogFields{
"remoteAddr": conn.RemoteAddr(),
},
)
return conn, err
} }
func (ts *TELNETServer) Handle(conn net.Conn) { func (ts *TELNETServer) Handle(conn net.Conn) {
@ -52,8 +43,7 @@ func (ts *TELNETServer) Handle(conn net.Conn) {
correlationID := watermill.NewUUID() correlationID := watermill.NewUUID()
ts.logger = ts.logger.With( ts.logger = ts.logger.With(
watermill.LogFields{ watermill.LogFields{
"correlation_id": correlationID, "correlation_id": correlationID},
},
) )
for s.Scan() { for s.Scan() {

Loading…
Cancel
Save