Skip to content

Commit

Permalink
feat: Gorm logger only logs on trace
Browse files Browse the repository at this point in the history
  • Loading branch information
laynax committed Feb 17, 2023
1 parent 4bf66ed commit 19d6969
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 29 deletions.
19 changes: 3 additions & 16 deletions pkg/logger/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package logger

import (
"context"
"errors"
"fmt"
"time"

"github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

Expand Down Expand Up @@ -53,25 +50,15 @@ func (g gormLogger) Trace(ctx context.Context, begin time.Time, fc func() (strin
},
})

if err == nil {
l.Tracef("%s", gormLoggerMsg)
if err != nil {
l.WithError(err).Tracef(gormLoggerMsg)

return
}

if errors.Is(err, gorm.ErrRecordNotFound) {
l.Tracef(err.Error())

return
}

l.WithError(err).Errorf(gormLoggerMsg)
l.Tracef(gormLoggerMsg)
}

func (g gormLogger) ParamsFilter(ctx context.Context, sql string, params ...interface{}) (string, []interface{}) {
if g.logger.Level() == logrus.TraceLevel {
return sql, params
}

return sql, nil
}
10 changes: 5 additions & 5 deletions pkg/logger/gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func TestNewGormLogger(t *testing.T) {
},
},
{
name: "Print on error log level with error",
isLogged: true,
name: "Empty on error log level with error",
isLogged: false,
resultError: true,
cfg: Config{
ConsoleEnabled: true,
Expand All @@ -80,8 +80,8 @@ func TestNewGormLogger(t *testing.T) {
},
},
{
name: "Print on debug log level with error",
isLogged: true,
name: "Empty on debug log level with error",
isLogged: false,
resultError: true,
cfg: Config{
ConsoleEnabled: true,
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestNewGormLogger(t *testing.T) {
assert.Contains(t, fields["sql"], "affected_rows")

if tt.resultError {
assert.Equal(t, fields["level"], "error")
assert.Equal(t, fields["level"], "trace")
assert.Contains(t, buffer.String(), "error")
assert.Equal(t, fields["error"], testDBErrorMsg)
} else {
Expand Down
4 changes: 0 additions & 4 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package logger

import "github.com/sirupsen/logrus"

type Level string

const (
Expand Down Expand Up @@ -34,8 +32,6 @@ const (
// Logger is the interface that defines the API/contract exposed by the
// SDK Logger.
type Logger interface {
// Level returns the current logging level.
Level() logrus.Level
// Panicf logs a message at level Panic.
Panicf(format string, args ...interface{})
// Fatalf logs a message at level Fatal.
Expand Down
4 changes: 0 additions & 4 deletions pkg/logger/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ type logrusLogEntry struct {
entry *logrus.Entry
}

func (l *logrusLogEntry) Level() logrus.Level {
return l.entry.Logger.GetLevel()
}

func (l *logrusLogEntry) Tracef(format string, args ...interface{}) {
l.entry.Tracef(format, args...)
}
Expand Down

0 comments on commit 19d6969

Please sign in to comment.