/
log.go
54 lines (45 loc) · 1.4 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package otgorm
import (
"context"
"fmt"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"gorm.io/gorm/logger"
)
// GormLogAdapter is an adapter between kitlog and gorm Logger interface
type GormLogAdapter struct {
Logging log.Logger
}
// LogMode implements logger.Interface
func (g GormLogAdapter) LogMode(logLevel logger.LogLevel) logger.Interface {
panic("Setting GORM LogMode is not allowed for kit log")
}
// Info implements logger.Interface
func (g GormLogAdapter) Info(ctx context.Context, s string, i ...interface{}) {
level.Info(g.Logging).Log("msg", fmt.Sprintf(s, i...))
}
// Warn implements logger.Interface
func (g GormLogAdapter) Warn(ctx context.Context, s string, i ...interface{}) {
level.Warn(g.Logging).Log("msg", fmt.Sprintf(s, i...))
}
// Error implements logger.Interface
func (g GormLogAdapter) Error(ctx context.Context, s string, i ...interface{}) {
level.Error(g.Logging).Log("msg", fmt.Sprintf(s, i...))
}
// Trace implements logger.Interface
func (g GormLogAdapter) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
sql, rows := fc()
elapsed := time.Since(begin)
var l log.Logger
if err == nil {
l = level.Debug(g.Logging)
} else {
l = level.Warn(g.Logging)
}
if rows == -1 {
l.Log("sql", sql, "duration", elapsed, "rows", "-", "err", err)
} else {
l.Log("sql", sql, "duration", elapsed, "rows", rows, "err", err)
}
}