/
logrus.go
177 lines (152 loc) · 4.24 KB
/
logrus.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
package logrusadapter
import (
"database/sql/driver"
"github.com/networkteam/go-sqllogger"
"github.com/sirupsen/logrus"
)
func NewSQLLogger(l *logrus.Logger, opts ...Opts) *SQLLogger {
var o Opts
switch len(opts) {
case 0:
o = DefaultOpts()
case 1:
o = opts[0]
default:
panic("expected zero or one opts")
}
return &SQLLogger{
logrusLogger: l,
opts: o,
}
}
type SQLLogger struct {
logrusLogger *logrus.Logger
opts Opts
}
var _ sqllogger.SQLLogger = SQLLogger{}
type Opts struct {
ConnectLevel logrus.Level
PrepareLevel logrus.Level
QueryLevel logrus.Level
ExecLevel logrus.Level
CloseLevel logrus.Level
TxLevel logrus.Level
}
func DefaultOpts() Opts {
return Opts{
ConnectLevel: logrus.DebugLevel,
PrepareLevel: logrus.DebugLevel,
QueryLevel: logrus.InfoLevel,
ExecLevel: logrus.InfoLevel,
CloseLevel: logrus.DebugLevel,
TxLevel: logrus.InfoLevel,
}
}
func (l SQLLogger) Connect(connID int64) {
l.logrusLogger.
WithField("connID", connID).
Log(l.opts.ConnectLevel, "DB Connect")
}
func (l SQLLogger) ConnBegin(connID, txID int64, opts driver.TxOptions) {
l.logrusLogger.
WithField("connID", connID).
Log(l.opts.TxLevel, "CONN Begin")
}
func (l SQLLogger) ConnPrepare(connID, stmtID int64, query string) {
l.logrusLogger.
WithField("connID", connID).
WithField("query", query).
WithField("stmtID", stmtID).
Log(l.opts.PrepareLevel, "CONN Prepare")
}
func (l SQLLogger) ConnPrepareContext(connID int64, stmtID int64, query string) {
l.logrusLogger.
WithField("connID", connID).
WithField("query", query).
WithField("stmtID", stmtID).
Log(l.opts.PrepareLevel, "CONN Prepare")
}
func (l SQLLogger) ConnQuery(connID, rowsID int64, query string, args []driver.Value) {
l.logrusLogger.
WithField("connID", connID).
WithField("query", query).
WithField("args", args).
WithField("rowsID", rowsID).
Log(l.opts.QueryLevel, "CONN Query")
}
func (l SQLLogger) ConnQueryContext(connID int64, rowsID int64, query string, args []driver.NamedValue) {
l.logrusLogger.
WithField("connID", connID).
WithField("query", query).
WithField("args", args).
WithField("rowsID", rowsID).
Log(l.opts.QueryLevel, "CONN Query")
}
func (l SQLLogger) ConnExec(connID int64, query string, args []driver.Value) {
l.logrusLogger.
WithField("connID", connID).
WithField("query", query).
Log(l.opts.ExecLevel, "CONN Exec")
}
func (l SQLLogger) ConnExecContext(connID int64, query string, args []driver.NamedValue) {
l.logrusLogger.
WithField("connID", connID).
WithField("query", query).
WithField("args", args).
Log(l.opts.ExecLevel, "CONN Exec")
}
func (l SQLLogger) ConnClose(connID int64) {
l.logrusLogger.
WithField("connID", connID).
Log(l.opts.CloseLevel, "CONN Close")
}
func (l SQLLogger) StmtExec(stmtID int64, query string, args []driver.Value) {
l.logrusLogger.
WithField("stmtID", stmtID).
WithField("query", query).
WithField("args", args).
Log(l.opts.ExecLevel, "STMT Exec")
}
func (l SQLLogger) StmtExecContext(stmtID int64, query string, args []driver.NamedValue) {
l.logrusLogger.
WithField("stmtID", stmtID).
WithField("query", query).
WithField("args", args).
Log(l.opts.ExecLevel, "STMT Exec")
}
func (l SQLLogger) StmtQuery(stmtID int64, rowsID int64, query string, args []driver.Value) {
l.logrusLogger.
WithField("stmtID", stmtID).
WithField("query", query).
WithField("args", args).
WithField("rowsID", rowsID).
Log(l.opts.QueryLevel, "STMT Query")
}
func (l SQLLogger) StmtQueryContext(stmtID int64, rowsID int64, query string, args []driver.NamedValue) {
l.logrusLogger.
WithField("stmtID", stmtID).
WithField("query", query).
WithField("args", args).
WithField("rowsID", rowsID).
Log(l.opts.QueryLevel, "STMT Query")
}
func (l SQLLogger) StmtClose(stmtID int64) {
l.logrusLogger.
WithField("stmtID", stmtID).
Log(l.opts.CloseLevel, "STMT Close")
}
func (l SQLLogger) RowsClose(rowsID int64) {
l.logrusLogger.
WithField("rowsID", rowsID).
Log(l.opts.CloseLevel, "ROWS Close")
}
func (l SQLLogger) TxCommit(txID int64) {
l.logrusLogger.
WithField("txID", txID).
Log(l.opts.TxLevel, "TX Commit")
}
func (l SQLLogger) TxRollback(txID int64) {
l.logrusLogger.
WithField("txID", txID).
Log(l.opts.TxLevel, "TX Rollback")
}