/
pgx.go
122 lines (106 loc) · 2.79 KB
/
pgx.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
package handlers
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/n-ask/fancylog"
"regexp"
"strings"
)
type PgLogger interface {
Debug(msg string, ctx ...interface{})
Info(msg string, ctx ...interface{})
Warn(msg string, ctx ...interface{})
Error(msg string, ctx ...interface{})
Crit(msg string, ctx ...interface{})
}
type Ignore struct {
}
type IgnoreStmtPrefix struct {
Text string
IgnoreCase bool
}
func (i IgnoreStmtPrefix) ContainsPrefix(sql string) bool {
if i.IgnoreCase {
return strings.HasPrefix(strings.ToUpper(sql), strings.ToUpper(i.Text))
}
return strings.HasPrefix(sql, i.Text)
}
type FancyPGLogger struct {
l fancylog.FancyLogger
ignores *[]IgnoreStmtPrefix
}
func NewFancyPGLogger(l fancylog.FancyLogger) *FancyPGLogger {
return &FancyPGLogger{l: l}
}
func (l *FancyPGLogger) SetIgnoreStmtPrefixes(p []IgnoreStmtPrefix) {
l.ignores = &p
}
func (l *FancyPGLogger) containsIgnoredPrefix(sql string) bool {
if l.ignores != nil {
for _, prefix := range *l.ignores {
if prefix.ContainsPrefix(sql) {
return true
}
}
}
return false
}
func (l *FancyPGLogger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
if val, ok := data["sql"]; ok {
sql := val.(string)
re_leadclose_whtsp := regexp.MustCompile(`^[\s\p{Zs}]+|[\s\p{Zs}]+$`)
re_inside_whtsp := regexp.MustCompile(`[\s\p{Zs}]{2,}`)
final := re_leadclose_whtsp.ReplaceAllString(sql, "")
final = re_inside_whtsp.ReplaceAllString(final, " ")
if l.containsIgnoredPrefix(sql) {
//Eat
return
}
data["sql"] = final
}
logArgs := make([]interface{}, 0, len(data))
for k, v := range data {
logArgs = append(logArgs, k, v)
}
switch level {
case pgx.LogLevelTrace:
l.Debug(msg, append(logArgs, "PGX_LOG_LEVEL", level)...)
case pgx.LogLevelDebug:
l.Debug(msg, logArgs...)
case pgx.LogLevelInfo, pgx.LogLevelNone:
l.Info(msg, logArgs...)
case pgx.LogLevelWarn:
l.Warn(msg, logArgs...)
case pgx.LogLevelError:
l.Error(msg, logArgs...)
default:
l.Error(msg, append(logArgs, "INVALID_PGX_LOG_LEVEL", level)...)
}
}
func (l *FancyPGLogger) Debug(msg string, ctx ...interface{}) {
l.l.DebugMap(toMap(msg, ctx))
}
func (l *FancyPGLogger) Info(msg string, ctx ...interface{}) {
l.l.InfoMap(toMap(msg, ctx))
}
func (l *FancyPGLogger) Warn(msg string, ctx ...interface{}) {
l.l.WarnMap(toMap(msg, ctx))
}
func (l *FancyPGLogger) Error(msg string, ctx ...interface{}) {
l.l.ErrorMap(toMap(msg, ctx))
}
func (l *FancyPGLogger) Crit(msg string, ctx ...interface{}) {
l.l.FatalMap(toMap(msg, ctx))
}
func toMap(msg string, ctx []interface{}) map[string]interface{} {
v := make(map[string]interface{})
v["msg"] = msg
if len(ctx)%2 == 0 {
for i := 0; i < len(ctx); i = i + 2 {
v[ctx[i].(string)] = ctx[i+1]
}
} else {
v["data"] = ctx
}
return v
}