-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
color.go
164 lines (147 loc) · 3.35 KB
/
color.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// =====================================
// Colorfy string by ANSI color
//
// inspired by github.com/fatih/color
// =====================================
package utils
import (
"fmt"
"strconv"
"strings"
"github.com/Laisky/zap"
"github.com/Laisky/zap/zapcore"
)
// ANSIColorEscape escape string for ANSI color
const ANSIColorEscape = "\x1b"
// Base attributes
const (
ANSIColorReset int = iota
ANSIColorBold
ANSIColorFaint
ANSIColorItalic
ANSIColorUnderline
ANSIColorBlinkSlow
ANSIColorBlinkRapid
ANSIColorReverseVideo
ANSIColorConcealed
ANSIColorCrossedOut
)
// Foreground text colors
const (
ANSIColorFgBlack int = iota + 30
ANSIColorFgRed
ANSIColorFgGreen
ANSIColorFgYellow
ANSIColorFgBlue
ANSIColorFgMagenta
ANSIColorFgCyan
ANSIColorFgWhite
)
// Foreground Hi-Intensity text colors
const (
ANSIColorFgHiBlack int = iota + 90
ANSIColorFgHiRed
ANSIColorFgHiGreen
ANSIColorFgHiYellow
ANSIColorFgHiBlue
ANSIColorFgHiMagenta
ANSIColorFgHiCyan
ANSIColorFgHiWhite
)
// Background text colors
const (
ANSIColorBgBlack int = iota + 40
ANSIColorBgRed
ANSIColorBgGreen
ANSIColorBgYellow
ANSIColorBgBlue
ANSIColorBgMagenta
ANSIColorBgCyan
ANSIColorBgWhite
)
// Background Hi-Intensity text colors
const (
ANSIColorBgHiBlack int = iota + 100
ANSIColorBgHiRed
ANSIColorBgHiGreen
ANSIColorBgHiYellow
ANSIColorBgHiBlue
ANSIColorBgHiMagenta
ANSIColorBgHiCyan
ANSIColorBgHiWhite
)
// Color wrap with ANSI color
func Color(color int, s string) string {
return fmt.Sprintf("\033[1;%dm%s\033[0m", color, s)
}
type gormLoggerItf interface {
Debug(string, ...zap.Field)
Info(string, ...zap.Field)
Error(string, ...zap.Field)
}
// GormLogger colored logger for gorm
type GormLogger struct {
logger gormLoggerItf
formatter func(...interface{}) []interface{}
}
// NewGormLogger new gorm sql logger
func NewGormLogger(formatter func(...interface{}) []interface{}, logger gormLoggerItf) *GormLogger {
return &GormLogger{
logger: logger,
formatter: formatter,
}
}
// Print print sql logger
func (l *GormLogger) Print(vs ...interface{}) {
fvs := l.formatter(vs...)
var fields []zapcore.Field
for i, v := range vs {
switch i {
case 0:
fields = append(fields, zap.Any("type", v))
case 1:
fields = append(fields, zap.Any("caller", v))
case 2:
fields = append(fields, zap.Any("ms", v))
case 3:
if len(fvs) < 4 {
fields = append(fields, zap.Any("sql", v))
}
case 4:
if len(fvs) < 4 {
fields = append(fields, zap.Any("args", v))
}
case 5:
fields = append(fields, zap.Any("affected", v))
default:
fields = append(fields, zap.Any(strconv.FormatInt(int64(i), 10), v))
}
}
if len(fvs) < 4 {
l.logger.Debug("", fields...)
return
}
var msg string
switch fvs[3].(type) {
case string:
msg = fvs[3].(string)
case []byte:
msg = string(fvs[3].([]byte))
default:
msg = fmt.Sprint(fvs[3])
}
switch strings.TrimSpace(strings.ToLower(strings.SplitN(msg, " ", 2)[0])) {
case "drop", "delete":
l.logger.Debug(Color(ANSIColorFgMagenta, msg), fields...)
case "insert":
l.logger.Debug(Color(ANSIColorFgGreen, msg), fields...)
case "update":
l.logger.Debug(Color(ANSIColorFgYellow, msg), fields...)
case "select":
l.logger.Debug(Color(ANSIColorFgCyan, msg), fields...)
case "error":
l.logger.Error(Color(ANSIColorFgHiRed, msg), fields...)
default:
l.logger.Debug(Color(ANSIColorFgBlue, msg), fields...)
}
}