/
logger.go
124 lines (109 loc) · 2.99 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
package db
import (
"database/sql"
"time"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
"github.com/volatiletech/sqlboiler/v4/boil"
)
func logQueryTime(logger *log.Logger, startTime time.Time) {
logger.Debugln("query took " + time.Since(startTime).String())
}
// QueryLogger is used to add extended logging for db queries in debug mode.
type QueryLogger struct {
DB *sqlx.DB
Logger *log.Logger
Enabled bool
Name string
}
// Query implementation of Executor
func (d *QueryLogger) Query(query string, args ...interface{}) (*sql.Rows, error) {
if d.Logger != nil {
if d.Logger != nil {
d.Logger.Debugln(query)
d.Logger.Debugln(args...)
defer logQueryTime(d.Logger, time.Now())
}
}
return d.DB.Query(query, args...)
}
// Exec implementation of Executor
func (d *QueryLogger) Exec(query string, args ...interface{}) (sql.Result, error) {
if d.Logger != nil {
if d.Logger != nil {
d.Logger.Debugln(query)
d.Logger.Debugln(args...)
defer logQueryTime(d.Logger, time.Now())
}
}
return d.DB.Exec(query, args...)
}
// QueryRow implementation of Executor
func (d *QueryLogger) QueryRow(query string, args ...interface{}) *sql.Row {
if d.Logger != nil {
d.Logger.Debugln(query)
d.Logger.Debugln(args...)
defer logQueryTime(d.Logger, time.Now())
}
return d.DB.QueryRow(query, args...)
}
// Begin implementation of Executor
func (d *QueryLogger) Begin() (boil.Transactor, error) {
if d.Logger != nil {
d.Logger.Debug("-> beginning tx")
}
tx, err := d.DB.Begin()
if err != nil {
return tx, err
}
return &queryLoggerTx{Tx: tx, logger: d.Logger}, nil
}
// Close implementation of Executor
func (d *QueryLogger) Close() error {
if d.Logger != nil {
d.Logger.Printf("closing %s db connection", d.Name)
}
return d.DB.Close()
}
type queryLoggerTx struct {
Tx *sql.Tx
logger *log.Logger
}
// Query implementation of Transaction Executor
func (t *queryLoggerTx) Query(query string, args ...interface{}) (*sql.Rows, error) {
if t.logger != nil {
t.logger.Debugln("-> " + query)
defer logQueryTime(t.logger, time.Now())
}
return t.Tx.Query(query, args...)
}
// Exec implementation of Transaction Executor
func (t *queryLoggerTx) Exec(query string, args ...interface{}) (sql.Result, error) {
if t.logger != nil {
t.logger.Debugln("-> " + query)
defer logQueryTime(t.logger, time.Now())
}
return t.Tx.Exec(query, args...)
}
// QueryRow implementation of Transaction Executor
func (t *queryLoggerTx) QueryRow(query string, args ...interface{}) *sql.Row {
if t.logger != nil {
t.logger.Debugln("-> " + query)
defer logQueryTime(t.logger, time.Now())
}
return t.Tx.QueryRow(query, args...)
}
//Commit implementation of Transaction Executor
func (t *queryLoggerTx) Commit() error {
if t.logger != nil {
t.logger.Debug("-> committing tx")
}
return t.Tx.Commit()
}
// Rollback implementation of Transaction Executor
func (t *queryLoggerTx) Rollback() error {
if t.logger != nil {
t.logger.Debug("-> rolling back tx")
}
return t.Tx.Rollback()
}