feat: Add structured logging for SQLBoiler
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user