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 ( import (
"context" "context"
"flag"
"fmt" "fmt"
"log" "log"
"net" "net"
"os"
"github.com/ThreeDotsLabs/watermill" "github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message" "github.com/ThreeDotsLabs/watermill/message"
@ -17,10 +19,16 @@ import (
) )
var ( var (
port int port int
verbose int
) )
func main() { 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)) l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil { if err != nil {
log.Fatalf("error binding to port %d\n: %s", port, err) log.Fatalf("error binding to port %d\n: %s", port, err)
@ -28,7 +36,7 @@ func main() {
defer l.Close() defer l.Close()
wmLogger := watermill.NewStdLogger(false, false) wmLogger := watermill.NewStdLogger(false, false)
wrappedSlogger := logger.NewWrappedSlogger() wrappedSlogger := logger.NewWrappedSlogger(os.Stdout, verbose)
router, err := message.NewRouter(message.RouterConfig{}, wrappedSlogger) router, err := message.NewRouter(message.RouterConfig{}, wrappedSlogger)
router.AddPlugin(plugin.SignalsHandler) router.AddPlugin(plugin.SignalsHandler)
@ -46,7 +54,7 @@ func main() {
go tp.Handle() go tp.Handle()
for { for {
conn, err := ts.l.Accept() conn, err := ts.Accept()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

@ -1,11 +1,16 @@
package logger package logger
import ( import (
"io"
"log/slog" "log/slog"
"github.com/ThreeDotsLabs/watermill" "github.com/ThreeDotsLabs/watermill"
) )
const (
LevelTrace = slog.Level(-5)
)
func LogFieldsToSlogAttrs(in watermill.LogFields) []slog.Attr { func LogFieldsToSlogAttrs(in watermill.LogFields) []slog.Attr {
attrs := make([]slog.Attr, 0) attrs := make([]slog.Attr, 0)
for k, v := range in { for k, v := range in {
@ -21,15 +26,19 @@ type WrappedSlogger struct {
*slog.Logger *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{ return &WrappedSlogger{
slog.Default(), 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)
wl.LogAttrs(nil, slog.Level(-5), msg, attrs...) wl.LogAttrs(nil, LevelTrace, msg, attrs...)
} }
func (wl *WrappedSlogger) Info(msg string, fields watermill.LogFields) { 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) { func (wl *WrappedSlogger) Trace(msg string, fields watermill.LogFields) {
attrs := LogFieldsToSlogAttrs(fields) 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 { 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 return &ts
} }
func (ts *TELNETServer) Accept() (net.Conn, error) {
return ts.l.Accept()
}
func (ts *TELNETServer) Handle(conn net.Conn) { func (ts *TELNETServer) Handle(conn net.Conn) {
s := bufio.NewScanner(conn) s := bufio.NewScanner(conn)
defer conn.Close() defer conn.Close()

Loading…
Cancel
Save