-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db/log): use logrus as logger for db (#1135)
* use logrus for gorm * use witherror * add knobs for db logging * false is default * show normal set of logrus levels * extend test struct with new fields * update test function with new fields
- Loading branch information
Showing
8 changed files
with
462 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package database | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
"gorm.io/gorm/utils" | ||
) | ||
|
||
// GormLogger is a custom logger for Gorm. | ||
type GormLogger struct { | ||
slowThreshold time.Duration | ||
skipErrRecordNotFound bool | ||
showSQL bool | ||
entry *logrus.Entry | ||
} | ||
|
||
// NewGormLogger creates a new Gorm logger. | ||
func NewGormLogger(logger *logrus.Entry, slowThreshold time.Duration, skipNotFound, showSQL bool) *GormLogger { | ||
return &GormLogger{ | ||
skipErrRecordNotFound: skipNotFound, | ||
slowThreshold: slowThreshold, | ||
showSQL: showSQL, | ||
entry: logger, | ||
} | ||
} | ||
|
||
// LogMode sets the log mode for the logger. | ||
func (l *GormLogger) LogMode(logger.LogLevel) logger.Interface { | ||
return l | ||
} | ||
|
||
// Info implements the logger.Interface. | ||
func (l *GormLogger) Info(ctx context.Context, msg string, args ...interface{}) { | ||
l.entry.WithContext(ctx).Info(msg, args) | ||
} | ||
|
||
// Warn implements the logger.Interface. | ||
func (l *GormLogger) Warn(ctx context.Context, msg string, args ...interface{}) { | ||
l.entry.WithContext(ctx).Warn(msg, args) | ||
} | ||
|
||
// Error implements the logger.Interface. | ||
func (l *GormLogger) Error(ctx context.Context, msg string, args ...interface{}) { | ||
l.entry.WithContext(ctx).Error(msg, args) | ||
} | ||
|
||
// Trace implements the logger.Interface. | ||
func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { | ||
elapsed := time.Since(begin) | ||
sql, rows := fc() | ||
fields := logrus.Fields{ | ||
"rows": rows, | ||
"elapsed": elapsed, | ||
"source": utils.FileWithLineNum(), | ||
} | ||
|
||
if l.showSQL { | ||
fields["sql"] = sql | ||
} | ||
|
||
if err != nil && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.skipErrRecordNotFound) { | ||
l.entry.WithContext(ctx).WithError(err).WithFields(fields).Error("gorm error") | ||
return | ||
} | ||
|
||
if l.slowThreshold != 0 && elapsed > l.slowThreshold { | ||
l.entry.WithContext(ctx).WithFields(fields).Warnf("gorm warn SLOW QUERY >= %s, took %s", l.slowThreshold, elapsed) | ||
return | ||
} | ||
|
||
l.entry.WithContext(ctx).WithFields(fields).Infof("gorm info") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package database | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestNewGormLogger(t *testing.T) { | ||
logger := logrus.NewEntry(logrus.New()) | ||
|
||
type args struct { | ||
logger *logrus.Entry | ||
slowThreshold time.Duration | ||
skipNotFound bool | ||
showSQL bool | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *GormLogger | ||
}{ | ||
{ | ||
name: "logger set", | ||
args: args{ | ||
logger: logger, | ||
slowThreshold: time.Second, | ||
skipNotFound: false, | ||
showSQL: true, | ||
}, | ||
want: &GormLogger{ | ||
slowThreshold: time.Second, | ||
skipErrRecordNotFound: false, | ||
showSQL: true, | ||
entry: logger, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if diff := cmp.Diff(NewGormLogger(tt.args.logger, tt.args.slowThreshold, tt.args.skipNotFound, tt.args.showSQL), tt.want, cmpopts.EquateComparable(GormLogger{})); diff != "" { | ||
t.Errorf("NewGormLogger() mismatch (-got +want):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.