From 6bd99f6da822c0c1fa110d5898e06e14121893cd Mon Sep 17 00:00:00 2001 From: Felicitas Pojtinger Date: Tue, 28 Dec 2021 23:16:33 +0100 Subject: [PATCH] feat: Add structured logging for SQLBoiler --- cmd/stfs/cmd/root.go | 2 ++ internal/logging/json.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/cmd/stfs/cmd/root.go b/cmd/stfs/cmd/root.go index 9f7e32d..f613ff5 100644 --- a/cmd/stfs/cmd/root.go +++ b/cmd/stfs/cmd/root.go @@ -8,6 +8,7 @@ import ( "github.com/pojntfx/stfs/internal/compression" "github.com/pojntfx/stfs/internal/encryption" + "github.com/pojntfx/stfs/internal/logging" "github.com/pojntfx/stfs/internal/signature" "github.com/pojntfx/stfs/pkg/config" "github.com/spf13/cobra" @@ -41,6 +42,7 @@ https://github.com/pojntfx/stfs`, if viper.GetBool(verboseFlag) { boil.DebugMode = true + boil.DebugWriter = logging.NewJSONLoggerWriter(4, "SQL Query", "query") } if err := compression.CheckCompressionFormat(viper.GetString(compressionFlag)); err != nil { diff --git a/internal/logging/json.go b/internal/logging/json.go index 9c3ce17..e883fc3 100644 --- a/internal/logging/json.go +++ b/internal/logging/json.go @@ -1,8 +1,10 @@ package logging import ( + "bufio" "encoding/json" "fmt" + "io" "os" "time" @@ -37,6 +39,22 @@ func NewJSONLogger(verbosity int) *JSONLogger { } } +func NewJSONLoggerWriter(verbosity int, event, key string) io.Writer { + jsonLogger := NewJSONLogger(verbosity) + + reader, writer := io.Pipe() + scanner := bufio.NewScanner(reader) + go func() { + for scanner.Scan() { + jsonLogger.Trace(event, map[string]interface{}{ + key: scanner.Text(), + }) + } + }() + + return writer +} + func (l JSONLogger) Trace(event string, keyvals ...interface{}) { if l.verbosity >= 4 { printJSON("TRACE", event, keyvals)