From ea393bbb58c2321c246278ac0d970bedfadbb674 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Tue, 27 Jun 2023 19:49:07 +0200 Subject: [PATCH] pretty listing of log data for testing --- lib/system/system.go | 9 --------- logging/logging.go | 14 ++++++++++++++ testing/testing.go | 22 ++++++++++++++++++---- 3 files changed, 32 insertions(+), 13 deletions(-) delete mode 100644 lib/system/system.go diff --git a/lib/system/system.go b/lib/system/system.go deleted file mode 100644 index 922d397..0000000 --- a/lib/system/system.go +++ /dev/null @@ -1,9 +0,0 @@ -package system - -import "git.sr.ht/~cco/go-scopes/lib" - -func RemoveFile(ctx lib.Context, fpath string) { -} - -func RemoveFiles(ctx lib.Context, path, pattern string) { -} diff --git a/logging/logging.go b/logging/logging.go index b847dd3..a5a93cf 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -1,6 +1,8 @@ package logging import ( + "encoding/json" + "fmt" "os" "path/filepath" @@ -73,6 +75,18 @@ func GetLogger(ctx lib.Context) *Logger { return globalLogger } +// processing and printing of log data + +func Parse(rec string) map[string]interface{} { + var buf map[string]interface{} + if err := json.Unmarshal([]byte(rec), &buf); err != nil { + fmt.Println("logging.Parse:", err) + } + return buf +} + +// set up static data + func init() { Levels["debug"] = DebugLevel Levels["info"] = InfoLevel diff --git a/testing/testing.go b/testing/testing.go index 5b08c65..dbc04ba 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -60,15 +60,29 @@ func (t *T) TearDownApp() { //t.AssertNoUncheckedMessages() } -func (t *T) LogCount(pr bool) (count int) { +func (t *T) LogCount(pr bool) int { + count := 0 f, _ := os.Open("log/scopes.log") defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { - if pr { - fmt.Println(scanner.Text()) - } count++ + if pr { + txt := scanner.Text() + data := logging.Parse(txt) + level := data["level"] + message := data["message"] + delete(data, "level") + delete(data, "message") + delete(data, "time") + srv := "-" + if data["service"] != nil { + srv = data["service"].(string) + delete(data, "service") + } + fmt.Printf("%d: %s: %s - %s; %+v\n", count, level, srv, message, data) + //fmt.Println(scanner.Text()) + } } return count }