feat: Add structured logging for SQLBoiler

This commit is contained in:
Felicitas Pojtinger
2021-12-28 23:16:33 +01:00
parent edde7a6a48
commit 6bd99f6da8
2 changed files with 20 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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)