-
Notifications
You must be signed in to change notification settings - Fork 8
/
logger.go
318 lines (271 loc) · 8.53 KB
/
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package sq
import (
"bytes"
"context"
"database/sql"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"time"
)
// QueryStats represents the statistics from running a query.
type QueryStats struct {
// Dialect of the query.
Dialect string
// Query string.
Query string
// Args slice provided with the query string.
Args []any
// Params maps param names back to arguments in the args slice (by index).
Params map[string][]int
// Err is the error from running the query.
Err error
// RowCount from running the query. Not valid for Exec().
RowCount sql.NullInt64
// RowsAffected by running the query. Not valid for
// FetchOne/FetchAll/FetchCursor.
RowsAffected sql.NullInt64
// LastInsertId of the query.
LastInsertId sql.NullInt64
// Exists is the result of FetchExists().
Exists sql.NullBool
// When the query started at.
StartedAt time.Time
// Time taken by the query.
TimeTaken time.Duration
// The caller file where the query was invoked.
CallerFile string
// The line in the caller file that invoked the query.
CallerLine int
// The name of the function where the query was invoked.
CallerFunction string
// The results from running the query (if it was provided).
Results string
}
// LogSettings are the various log settings taken into account when producing
// the QueryStats.
type LogSettings struct {
// Dispatch logging asynchronously (logs may arrive out of order which can be confusing, but it won't block function calls).
LogAsynchronously bool
// Include time taken by the query.
IncludeTime bool
// Include caller (filename and line number).
IncludeCaller bool
// Include fetched results.
IncludeResults int
}
// SqLogger represents a logger for the sq package.
type SqLogger interface {
// SqLogSettings should populate a LogSettings struct, which influences
// what is added into the QueryStats.
SqLogSettings(context.Context, *LogSettings)
// SqLogQuery logs a query when for the given QueryStats.
SqLogQuery(context.Context, QueryStats)
}
type sqLogger struct {
logger *log.Logger
config LoggerConfig
}
// LoggerConfig is the config used for the sq logger.
type LoggerConfig struct {
// Dispatch logging asynchronously (logs may arrive out of order which can be confusing, but it won't block function calls).
LogAsynchronously bool
// Show time taken by the query.
ShowTimeTaken bool
// Show caller (filename and line number).
ShowCaller bool
// Show fetched results.
ShowResults int
// If true, logs are shown as plaintext (no color).
NoColor bool
// Verbose query interpolation, which shows the query before and after
// interpolating query arguments. The logged query is interpolated by
// default, InterpolateVerbose only controls whether the query before
// interpolation is shown. To disable query interpolation entirely, look at
// HideArgs.
InterpolateVerbose bool
// Explicitly hides arguments when logging the query (only the query
// placeholders will be shown).
HideArgs bool
}
var _ SqLogger = (*sqLogger)(nil)
var defaultLogger = NewLogger(os.Stdout, "", log.LstdFlags, LoggerConfig{
ShowTimeTaken: true,
ShowCaller: true,
})
var verboseLogger = NewLogger(os.Stdout, "", log.LstdFlags, LoggerConfig{
ShowTimeTaken: true,
ShowCaller: true,
ShowResults: 5,
InterpolateVerbose: true,
})
// NewLogger returns a new SqLogger.
func NewLogger(w io.Writer, prefix string, flag int, config LoggerConfig) SqLogger {
return &sqLogger{
logger: log.New(w, prefix, flag),
config: config,
}
}
// SqLogSettings implements the SqLogger interface.
func (l *sqLogger) SqLogSettings(ctx context.Context, settings *LogSettings) {
settings.LogAsynchronously = l.config.LogAsynchronously
settings.IncludeTime = l.config.ShowTimeTaken
settings.IncludeCaller = l.config.ShowCaller
settings.IncludeResults = l.config.ShowResults
}
// SqLogQuery implements the SqLogger interface.
func (l *sqLogger) SqLogQuery(ctx context.Context, queryStats QueryStats) {
var reset, red, green, blue, purple string
envNoColor, _ := strconv.ParseBool(os.Getenv("NO_COLOR"))
if !l.config.NoColor && !envNoColor {
reset = colorReset
red = colorRed
green = colorGreen
blue = colorBlue
purple = colorPurple
}
buf := bufpool.Get().(*bytes.Buffer)
buf.Reset()
defer bufpool.Put(buf)
if queryStats.Err == nil {
buf.WriteString(green + "[OK]" + reset)
} else {
buf.WriteString(red + "[FAIL]" + reset)
}
if l.config.HideArgs {
buf.WriteString(" " + queryStats.Query + ";")
} else if !l.config.InterpolateVerbose {
if queryStats.Err != nil {
buf.WriteString(" " + queryStats.Query + ";")
if len(queryStats.Args) > 0 {
buf.WriteString(" [")
}
for i := 0; i < len(queryStats.Args); i++ {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(fmt.Sprintf("%#v", queryStats.Args[i]))
}
if len(queryStats.Args) > 0 {
buf.WriteString("]")
}
} else {
query, err := Sprintf(queryStats.Dialect, queryStats.Query, queryStats.Args)
if err != nil {
query += " " + err.Error()
}
buf.WriteString(" " + query + ";")
}
}
if queryStats.Err != nil {
errStr := queryStats.Err.Error()
if i := strings.IndexByte(errStr, '\n'); i < 0 {
buf.WriteString(blue + " err" + reset + "={" + queryStats.Err.Error() + "}")
}
}
if l.config.ShowTimeTaken {
buf.WriteString(blue + " timeTaken" + reset + "=" + queryStats.TimeTaken.String())
}
if queryStats.RowCount.Valid {
buf.WriteString(blue + " rowCount" + reset + "=" + strconv.FormatInt(queryStats.RowCount.Int64, 10))
}
if queryStats.RowsAffected.Valid {
buf.WriteString(blue + " rowsAffected" + reset + "=" + strconv.FormatInt(queryStats.RowsAffected.Int64, 10))
}
if queryStats.LastInsertId.Valid {
buf.WriteString(blue + " lastInsertId" + reset + "=" + strconv.FormatInt(queryStats.LastInsertId.Int64, 10))
}
if queryStats.Exists.Valid {
buf.WriteString(blue + " exists" + reset + "=" + strconv.FormatBool(queryStats.Exists.Bool))
}
if l.config.ShowCaller {
buf.WriteString(blue + " caller" + reset + "=" + queryStats.CallerFile + ":" + strconv.Itoa(queryStats.CallerLine) + ":" + filepath.Base(queryStats.CallerFunction))
}
if !l.config.HideArgs && l.config.InterpolateVerbose {
buf.WriteString("\n" + purple + "----[ Executing query ]----" + reset)
buf.WriteString("\n" + queryStats.Query + "; " + fmt.Sprintf("%#v", queryStats.Args))
buf.WriteString("\n" + purple + "----[ with bind values ]----" + reset)
query, err := Sprintf(queryStats.Dialect, queryStats.Query, queryStats.Args)
query += ";"
if err != nil {
query += " " + err.Error()
}
buf.WriteString("\n" + query)
}
if l.config.ShowResults > 0 && queryStats.Err == nil {
buf.WriteString("\n" + purple + "----[ Fetched result ]----" + reset)
buf.WriteString(queryStats.Results)
if queryStats.RowCount.Int64 > int64(l.config.ShowResults) {
buf.WriteString("\n...\n(Fetched " + strconv.FormatInt(queryStats.RowCount.Int64, 10) + " rows)")
}
}
if buf.Len() > 0 {
l.logger.Println(buf.String())
}
}
// Log wraps a DB and adds logging to it.
func Log(db DB) interface {
DB
SqLogger
} {
return struct {
DB
SqLogger
}{DB: db, SqLogger: defaultLogger}
}
// VerboseLog wraps a DB and adds verbose logging to it.
func VerboseLog(db DB) interface {
DB
SqLogger
} {
return struct {
DB
SqLogger
}{DB: db, SqLogger: verboseLogger}
}
var defaultLogSettings atomic.Value
// SetDefaultLogSettings sets the function to configure the default
// LogSettings. This value is not used unless SetDefaultLogQuery is also
// configured.
func SetDefaultLogSettings(logSettings func(context.Context, *LogSettings)) {
defaultLogSettings.Store(logSettings)
}
var defaultLogQuery atomic.Value
// SetDefaultLogQuery sets the default logging function to call for all
// queries (if a logger is not explicitly passed in).
func SetDefaultLogQuery(logQuery func(context.Context, QueryStats)) {
defaultLogQuery.Store(logQuery)
}
type sqLogStruct struct {
logSettings func(context.Context, *LogSettings)
logQuery func(context.Context, QueryStats)
}
var _ SqLogger = (*sqLogStruct)(nil)
func (l *sqLogStruct) SqLogSettings(ctx context.Context, logSettings *LogSettings) {
if l.logSettings == nil {
return
}
l.logSettings(ctx, logSettings)
}
func (l *sqLogStruct) SqLogQuery(ctx context.Context, queryStats QueryStats) {
if l.logQuery == nil {
return
}
l.logQuery(ctx, queryStats)
}
const (
colorReset = "\x1b[0m"
colorRed = "\x1b[91m"
colorGreen = "\x1b[92m"
colorYellow = "\x1b[93m"
colorBlue = "\x1b[94m"
colorPurple = "\x1b[95m"
colorCyan = "\x1b[96m"
colorGray = "\x1b[97m"
colorWhite = "\x1b[97m"
)