-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
46 lines (36 loc) · 1005 Bytes
/
logger.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
package models
import (
"context"
"errors"
"time"
"github.com/rs/zerolog"
gorm_logger "gorm.io/gorm/logger"
)
type logger struct {
Logger zerolog.Logger
}
func (l *logger) LogMode(gorm_logger.LogLevel) gorm_logger.Interface {
return l
}
func (l *logger) Info(_ context.Context, s string, args ...interface{}) {
l.Logger.Info().Msgf(s, args...)
}
func (l *logger) Warn(_ context.Context, s string, args ...interface{}) {
l.Logger.Warn().Msgf(s, args...)
}
func (l *logger) Error(_ context.Context, s string, args ...interface{}) {
l.Logger.Error().Msgf(s, args...)
}
func (l *logger) Trace(_ context.Context, begin time.Time, fc func() (string, int64), err error) {
elapsed := time.Since(begin)
sql, _ := fc()
fields := map[string]interface{}{
"sql": sql,
"duration": elapsed,
}
if err != nil && !errors.Is(err, ErrResourceNotFound) {
l.Logger.Error().Err(err).Fields(fields).Msg("[GORM] query error")
return
}
l.Logger.Debug().Fields(fields).Msgf("[GORM] query")
}