aboutsummaryrefslogtreecommitdiff
path: root/pkg/ext/gorm_logger.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/ext/gorm_logger.go')
-rw-r--r--pkg/ext/gorm_logger.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/pkg/ext/gorm_logger.go b/pkg/ext/gorm_logger.go
new file mode 100644
index 0000000..bfb26d2
--- /dev/null
+++ b/pkg/ext/gorm_logger.go
@@ -0,0 +1,58 @@
+package ext
+
+import (
+ "context"
+ "fmt"
+ "time"
+
+ "github.com/sirupsen/logrus"
+ "gorm.io/gorm/logger"
+ "gorm.io/gorm/utils"
+)
+
+type Log struct {
+ logrus *logrus.Entry
+}
+
+func getFullMsg(msg string, data ...interface{}) string {
+ return fmt.Sprintf(msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
+}
+
+func (self *Log) LogMode(log logger.LogLevel) logger.Interface {
+ return self
+}
+
+func (self *Log) Info(ctx context.Context, msg string, data ...interface{}) {
+ fullMsg := getFullMsg(msg, data)
+ self.logrus.
+ WithContext(ctx).
+ Info(fullMsg)
+}
+
+func (self *Log) Warn(ctx context.Context, msg string, data ...interface{}) {
+ fullMsg := getFullMsg(msg, data)
+ self.logrus.
+ WithContext(ctx).
+ Warn(fullMsg)
+}
+func (self *Log) Error(ctx context.Context, msg string, data ...interface{}) {
+ fullMsg := getFullMsg(msg, data)
+ self.logrus.
+ WithContext(ctx).
+ Error(fullMsg)
+}
+
+func (self *Log) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
+ elapsed := time.Since(begin)
+ sql, _ := fc()
+ self.logrus.
+ WithContext(ctx).
+ WithField("time", elapsed).
+ Printf(sql)
+}
+
+func Wraplog(log *logrus.Entry) *Log {
+ return &Log{
+ logrus: log,
+ }
+}