From 4748dab2ff7793fd395eb4fa56bf5367a1dcb2e9 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Sat, 1 Jul 2023 18:14:35 -0400 Subject: [PATCH] now i'm using zap --- lexer.go | 22 ++++++++++++++++++---- lexer_test.go | 1 + 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lexer.go b/lexer.go index e206f6a..2010973 100644 --- a/lexer.go +++ b/lexer.go @@ -3,10 +3,11 @@ package wikilink import ( "fmt" - "log" "strings" // "unicode" "unicode/utf8" + + "go.uber.org/zap" ) const ( @@ -35,6 +36,7 @@ const ( func Lex(name, input string) *Lexer { l := &Lexer{ + L: zap.NewExample().Sugar().Named("lexer"), name: name, input: input, state: lexText, @@ -72,6 +74,7 @@ func (l *Lexer) backup() { } type Lexer struct { + L *zap.SugaredLogger name, input string start, pos, width int state stateFn @@ -103,18 +106,29 @@ func (l *Lexer) acceptRun(valid string) { func (l *Lexer) emit(t ItemType) { i := Item{t, l.input[l.start:l.pos]} - log.Printf("emitting Item: %s\n", i.String()) + L := l.L.With( + zap.Int("pos", l.pos), + zap.Int("width", l.width), + ).Named("emit") + + L.Debugw("emitting item", + zap.String("item", i.String()), + ) l.items <- i l.start = l.pos } func (l *Lexer) errorf(format string, args ...interface{}) stateFn { - log.Printf("entering errorf: %q\n", format) - l.items <- Item{ + L := l.L.Named("errorf") + errorItem := Item{ ItemError, fmt.Sprintf(format, args...), } + L.Debugw("emitting errorItem", + zap.String("error", errorItem.String()), + ) + l.items <- errorItem return nil } diff --git a/lexer_test.go b/lexer_test.go index 8c0fa3e..ad7042a 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -93,6 +93,7 @@ func Test_Lexer(t *testing.T) { t.Run(tc.name, func(t *testing.T) { // t.Parallel() l := wikilink.Lex("testLexer", tc.in) + defer l.L.Sync() for _, e := range tc.expected { n := l.NextItem() if e.Typ != n.Typ {