Skip to content

Commit

Permalink
Merge pull request #1103 from France-ioi/use_SharedLogger_in_db_loggers
Browse files Browse the repository at this point in the history
Do not store the logger inside DBLogger & StructuredDBLogger
  • Loading branch information
zenovich authored Jul 15, 2024
2 parents 91bce76 + 9052e6f commit 6f39d95
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
12 changes: 9 additions & 3 deletions app/logging/db_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ type DBLogger interface {
Print(v ...interface{})
}

type sharedLoggerWriter struct{}

func (l *sharedLoggerWriter) Println(v ...interface{}) {
SharedLogger.Println(v...)
}

// NewDBLogger returns a logger for the database and the `logMode` as well as the 'rawLogMode', according to the config.
func (l *Logger) NewDBLogger() (DBLogger, bool, bool) {
if l.config == nil {
// if cannot parse config, log on error to stdout
return gorm.Logger{LogWriter: l}, false, false
return gorm.Logger{LogWriter: &sharedLoggerWriter{}}, false, false
}

logMode := l.config.GetBool("LogSQLQueries")
rawLogMode := l.config.GetBool("LogRawSQLQueries")
switch l.config.GetString("format") {
case formatText:
return gorm.Logger{LogWriter: l}, logMode, rawLogMode
return gorm.Logger{LogWriter: &sharedLoggerWriter{}}, logMode, rawLogMode
case formatJSON:
return NewStructuredDBLogger(l.Logger), logMode, rawLogMode
return NewStructuredDBLogger(), logMode, rawLogMode
default:
panic("Logging format must be either 'text' or 'json'. Got: " + l.config.GetString("format"))
}
Expand Down
24 changes: 16 additions & 8 deletions app/logging/raw_db_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
)

func TestNewRawDBLogger_TextLog(t *testing.T) {
nulllogger, hook := loggingtest.NewNullLogger()
var hook *loggingtest.Hook
SharedLogger.Logger, hook = loggingtest.NewNullLogger()
defer ResetShared()
config := viper.New()
config.Set("Format", "text")
config.Set("LogRawSQLQueries", true)
logger := &Logger{nulllogger, config}
logger := &Logger{SharedLogger.Logger, config}
dbLogger, _, rawLogMode := logger.NewDBLogger()

rawLogger := NewRawDBLogger(dbLogger, rawLogMode)
Expand All @@ -25,23 +27,27 @@ func TestNewRawDBLogger_TextLog(t *testing.T) {
}

func TestNewRawDBLogger_HonoursLogMode(t *testing.T) {
nulllogger, hook := loggingtest.NewNullLogger()
var hook *loggingtest.Hook
SharedLogger.Logger, hook = loggingtest.NewNullLogger()
defer ResetShared()
config := viper.New()
config.Set("Format", "text")
config.Set("LogRawSQLQueries", false)
logger := &Logger{nulllogger, config}
logger := &Logger{SharedLogger.Logger, config}
dbLogger, _, rawLogMode := logger.NewDBLogger()
rawLogger := NewRawDBLogger(dbLogger, rawLogMode)
rawLogger.Log(context.TODO(), "some message", "err", nil)
assert.Empty(t, hook.GetAllStructuredLogs())
}

func TestNewRawDBLogger_JSONLog(t *testing.T) {
nulllogger, hook := loggingtest.NewNullLogger()
var hook *loggingtest.Hook
SharedLogger.Logger, hook = loggingtest.NewNullLogger()
defer ResetShared()
config := viper.New()
config.Set("Format", "json")
config.Set("LogRawSQLQueries", true)
logger := &Logger{nulllogger, config}
logger := &Logger{SharedLogger.Logger, config}
dbLogger, _, rawLogMode := logger.NewDBLogger()
rawLogger := NewRawDBLogger(dbLogger, rawLogMode)
rawLogger.Log(context.TODO(), "some message", "err", nil)
Expand All @@ -50,11 +56,13 @@ func TestNewRawDBLogger_JSONLog(t *testing.T) {
}

func TestRawDBLogger_ShouldSkipSkippedActions(t *testing.T) {
nulllogger, hook := loggingtest.NewNullLogger()
var hook *loggingtest.Hook
SharedLogger.Logger, hook = loggingtest.NewNullLogger()
defer ResetShared()
config := viper.New()
config.Set("Format", "json")
config.Set("LogRawSQLQueries", true)
logger := &Logger{nulllogger, config}
logger := &Logger{SharedLogger.Logger, config}
dbLogger, _, rawLogMode := logger.NewDBLogger()
rawLogger := NewRawDBLogger(dbLogger, rawLogMode)
rawLogger.Log(context.TODO(), "sql-stmt-exec", "err", driver.ErrSkip)
Expand Down
12 changes: 4 additions & 8 deletions app/logging/structuredDBLogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ import (
"regexp"
"strings"
"time"

"github.com/sirupsen/logrus" //nolint:depguard
)

// StructuredDBLogger is a database structured logger.
type StructuredDBLogger struct {
logger *logrus.Logger
}
type StructuredDBLogger struct{}

// NewStructuredDBLogger created a database structured logger.
func NewStructuredDBLogger(logger *logrus.Logger) *StructuredDBLogger {
return &StructuredDBLogger{logger}
func NewStructuredDBLogger() *StructuredDBLogger {
return &StructuredDBLogger{}
}

var (
Expand All @@ -29,7 +25,7 @@ var (
// values: 0: level, 1: source file, 2: duration in ns, 3: query, 4: slice of parameters, 5: rows affected or returned
func (l *StructuredDBLogger) Print(values ...interface{}) {
level := values[0]
logger := l.logger.WithField("type", "db")
logger := SharedLogger.WithField("type", "db")

switch level {
case "sql":
Expand Down
6 changes: 4 additions & 2 deletions app/logging/structuredDBLogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ func TestStructuredDBLogger_Print_SQLError(t *testing.T) {

func TestStructuredDBLogger_Print_RawSQLWithDuration(t *testing.T) {
assert := assertlib.New(t)
logger, hook := test.NewNullLogger()
structuredLogger := logging.NewStructuredDBLogger(logger)
var hook *test.Hook
logging.SharedLogger.Logger, hook = test.NewNullLogger()
defer logging.ResetShared()
structuredLogger := logging.NewStructuredDBLogger()
structuredLogger.Print("rawsql", nil, "sql-stmt-exec",
map[string]interface{}{
"query": "SELECT 1",
Expand Down

0 comments on commit 6f39d95

Please sign in to comment.