Compare commits

...

4 Commits
main ... dev

Author SHA1 Message Date
Nick Dumas 7766253319 adding more tracing 1 year ago
Nick Dumas 9eadcedb0b fixing a subtle bug in With() 1 year ago
Nick Dumas 07e6cab5cd significant rewrite of wrapped logger
i always want traces emitted for a few reasons
- debugging. never know when something will break.
- at some point i'm gonna want to capture and replay sessions for
  testing purposes. the trace log will make that easier
1 year ago
Nick Dumas 484fef459f make this simpler 1 year ago

3
.gitignore vendored

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

@ -36,7 +36,28 @@ func main() {
defer l.Close()
wmLogger := watermill.NewStdLogger(false, false)
wrappedSlogger := logger.NewWrappedSlogger(os.Stdout, verbose)
traceFile, err := os.OpenFile("trace.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
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.AddPlugin(plugin.SignalsHandler)
@ -51,6 +72,8 @@ func main() {
ctx := context.Background()
ts := telnet.NewTELNETServer(ctx, l, pubSub, wrappedSlogger)
tp := telnet.NewTELNETParser(ctx, pubSub, wrappedSlogger)
wrappedSlogger.Info("booting up, welcome back formers", nil)
go tp.Handle()
for {

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

@ -35,6 +35,10 @@ func NewTELNETParser(c context.Context, s message.Subscriber, wml watermill.Logg
return &TELNETParser{
c: c,
s: s,
logger: wml,
logger: wml.With(
watermill.LogFields{
"name": "TELNETParser",
},
),
}
}

@ -24,14 +24,23 @@ func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher, wml
c: c,
l: l,
p: p,
logger: wml,
logger: wml.With(
watermill.LogFields{
"name": "TELNETServer",
},
),
}
return &ts
}
func (ts *TELNETServer) Accept() (net.Conn, error) {
return ts.l.Accept()
conn, err := ts.l.Accept()
ts.logger.Trace("receiving telnet connection", watermill.LogFields{
"remoteAddr": conn.RemoteAddr(),
},
)
return conn, err
}
func (ts *TELNETServer) Handle(conn net.Conn) {
@ -43,7 +52,8 @@ func (ts *TELNETServer) Handle(conn net.Conn) {
correlationID := watermill.NewUUID()
ts.logger = ts.logger.With(
watermill.LogFields{
"correlation_id": correlationID},
"correlation_id": correlationID,
},
)
for s.Scan() {

Loading…
Cancel
Save