adding flags for log verbosity, cleaning up refactor

main
Nick Dumas 1 year ago
parent d21c9cf66c
commit 3e42ccb6f6

@ -2,9 +2,11 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
@ -17,10 +19,16 @@ import (
)
var (
port int
port int
verbose int
)
func main() {
flag.IntVar(&verbose, "verbose", 0, "-5:Trace, -4:Debug, 0:Info, 4:Warn, 8:Error")
flag.IntVar(&port, "port", 5555, "MUD server port")
flag.Parse()
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("error binding to port %d\n: %s", port, err)
@ -28,7 +36,7 @@ func main() {
defer l.Close()
wmLogger := watermill.NewStdLogger(false, false)
wrappedSlogger := logger.NewWrappedSlogger()
wrappedSlogger := logger.NewWrappedSlogger(os.Stdout, verbose)
router, err := message.NewRouter(message.RouterConfig{}, wrappedSlogger)
router.AddPlugin(plugin.SignalsHandler)
@ -46,7 +54,7 @@ func main() {
go tp.Handle()
for {
conn, err := ts.l.Accept()
conn, err := ts.Accept()
if err != nil {
log.Fatal(err)
}

@ -1,11 +1,16 @@
package logger
import (
"io"
"log/slog"
"github.com/ThreeDotsLabs/watermill"
)
const (
LevelTrace = slog.Level(-5)
)
func LogFieldsToSlogAttrs(in watermill.LogFields) []slog.Attr {
attrs := make([]slog.Attr, 0)
for k, v := range in {
@ -21,15 +26,19 @@ type WrappedSlogger struct {
*slog.Logger
}
func NewWrappedSlogger() *WrappedSlogger {
func NewWrappedSlogger(w io.Writer, verbose int) *WrappedSlogger {
opts := &slog.HandlerOptions{
Level: slog.Level(verbose),
}
handler := slog.NewTextHandler(w, opts)
return &WrappedSlogger{
slog.Default(),
slog.New(handler),
}
}
func (wl *WrappedSlogger) Error(msg string, err error, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields)
wl.LogAttrs(nil, slog.Level(-5), msg, attrs...)
wl.LogAttrs(nil, LevelTrace, msg, attrs...)
}
func (wl *WrappedSlogger) Info(msg string, fields watermill.LogFields) {
@ -44,7 +53,7 @@ func (wl *WrappedSlogger) Debug(msg string, fields watermill.LogFields) {
func (wl *WrappedSlogger) Trace(msg string, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields)
wl.LogAttrs(nil, slog.Level(-5), msg, attrs...)
wl.LogAttrs(nil, LevelTrace, msg, attrs...)
}
func (wl *WrappedSlogger) With(fields watermill.LogFields) watermill.LoggerAdapter {

@ -30,6 +30,10 @@ func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher, wml
return &ts
}
func (ts *TELNETServer) Accept() (net.Conn, error) {
return ts.l.Accept()
}
func (ts *TELNETServer) Handle(conn net.Conn) {
s := bufio.NewScanner(conn)
defer conn.Close()

Loading…
Cancel
Save