-
Notifications
You must be signed in to change notification settings - Fork 8
/
query_logger.go
136 lines (124 loc) · 3.89 KB
/
query_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package beeorm
import (
"fmt"
"log"
"math"
"strconv"
"strings"
"time"
)
type QueryLoggerSource int
const sourceMySQL = "mysql"
const sourceRedis = "redis"
const sourceLocalCache = "local_cache"
const beeORMLogo = "\u001B[1m\x1b[38;2;0;0;0;48;2;255;255;255mBee\u001B[38;2;254;147;51mORM \u001B[0m\x1b[0m\u001B[0m"
const mysqlLogo = "\x1b[38;2;2;117;143;48;2;255;255;255mMy\u001B[38;2;242;145;17mSQL \u001B[0m\x1b[0m\u001B[0m"
const redisLogo = "\u001B[1m\x1b[38;2;191;56;42;48;2;255;255;255mredis \u001B[0m\x1b[0m\u001B[0m"
const localCacheLogo = "\u001B[1m\x1b[38;2;254;147;51;48;2;255;255;255mlocal \u001B[0m\x1b[0m\u001B[0m"
const timeTemplate = "\x1b[38;2;0;0;0;48;2;255;%d;%dm %0.1fms%s \u001B[0m\x1b[0m\u001B[0m\n"
const operationTemplate = "\u001B[1m\x1b[38;2;0;0;0;48;2;255;255;255m%-14s\u001B[0m\x1b[0m\u001B[0m"
const queryTemplate = "\x1b[38;2;255;255;155m%s\u001B[0m\x1b[0m\u001B[0m\n"
const errorTemplate = "\x1b[38;2;191;46;42m%s\u001B[0m\x1b[0m\u001B[0m\n"
type defaultLogLogger struct {
maxPoolLen int
logger *log.Logger
}
func (d *defaultLogLogger) Handle(_ ORM, fields map[string]any) {
row := beeORMLogo
switch fields["source"] {
case "mysql":
row += mysqlLogo
case "redis":
row += redisLogo
case "local_cache":
row += localCacheLogo
}
poolTemplate := "\u001B[1m\x1b[38;2;175;175;175;48;2;255;255;255m%-" + strconv.Itoa(d.maxPoolLen+3) + "s\u001B[0m\x1b[0m\u001B[0m"
row += fmt.Sprintf(poolTemplate, fields["pool"])
microseconds := float64(0)
microsecondsVal, has := fields["microseconds"]
timeSuffix := ""
timeBackground := 255
if has {
microseconds = float64(microsecondsVal.(int64))
timeBackground -= int(microseconds / 2400)
timeBackground = int(math.Max(float64(0), float64(timeBackground)))
timeSuffix = strings.Repeat(" ", int(microseconds/10000))
}
seconds := microseconds / 1000
row += fmt.Sprintf(operationTemplate, fields["operation"])
row += fmt.Sprintf(timeTemplate, timeBackground, timeBackground, seconds, timeSuffix)
row += fmt.Sprintf(queryTemplate, fields["query"])
err, hasError := fields["error"]
if hasError {
row += fmt.Sprintf(errorTemplate, err)
}
d.logger.Print(row)
}
type LogHandler interface {
Handle(orm ORM, log map[string]any)
}
func (orm *ormImplementation) RegisterQueryLogger(handler LogHandler, mysql, redis, local bool) {
orm.mutexData.Lock()
defer orm.mutexData.Unlock()
if mysql {
orm.hasDBLogger = true
orm.queryLoggersDB = orm.appendLog(orm.queryLoggersDB, handler)
}
if redis {
orm.hasRedisLogger = true
orm.queryLoggersRedis = orm.appendLog(orm.queryLoggersRedis, handler)
}
if local {
orm.hasLocalCacheLogger = true
orm.queryLoggersLocalCache = orm.appendLog(orm.queryLoggersLocalCache, handler)
}
}
func (orm *ormImplementation) EnableQueryDebug() {
orm.EnableQueryDebugCustom(true, true, true)
}
func (orm *ormImplementation) EnableQueryDebugCustom(mysql, redis, local bool) {
orm.RegisterQueryLogger(orm.engine.Registry().getDefaultQueryLogger(), mysql, redis, local)
}
func getNow(has bool) *time.Time {
if !has {
return nil
}
s := time.Now()
return &s
}
func (orm *ormImplementation) appendLog(logs []LogHandler, toAdd LogHandler) []LogHandler {
for _, v := range logs {
if v == toAdd {
return logs
}
}
return append(logs, toAdd)
}
func fillLogFields(orm ORM, handlers []LogHandler, pool, source, operation, query string, start *time.Time, cacheMiss bool, err error) {
fields := map[string]any{
"operation": operation,
"query": query,
"pool": pool,
"source": source,
}
if cacheMiss {
fields["miss"] = "TRUE"
}
meta := orm.GetMetaData()
if len(meta) > 0 {
fields["meta"] = meta
}
if start != nil {
now := time.Now()
fields["microseconds"] = time.Since(*start).Microseconds()
fields["started"] = start.UnixNano()
fields["finished"] = now.UnixNano()
}
if err != nil {
fields["error"] = err.Error()
}
for _, handler := range handlers {
handler.Handle(orm, fields)
}
}