-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
260 lines (239 loc) · 6.65 KB
/
report.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
package report
import (
"errors"
"fmt"
"log"
"net/mail"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/kaibox-git/lmail"
"github.com/kaibox-git/sqlparams"
)
var (
QueryTimeWarning = 200 * time.Millisecond // Высылает предупреждение, если время выполнения запроса превышает установленное
QueryDeadline = 5 * time.Second // Прерывает запрос, освобождая соединение, если время выполнения запроса превышает установленное
ErrReported = errors.New(`error has been reported`)
ErrNotFound = errors.New("not found")
ErrContext = errors.New(`context canceled`)
ErrInternal = errors.New("internal server error")
)
type ReportProvider interface {
Message(m string)
Sql(query string, params ...interface{})
SqlError(r interface{}, err error, query string, params ...interface{})
Error(r interface{}, err error)
FileWithLineNum() string
FilesWithLineNum() (out []string)
}
type Report struct {
appName string
email emailConfig
errorLog *log.Logger
}
type emailConfig struct {
sender lmail.EmailProvider
from mail.Address
to []mail.Address
}
func New(appName string, mailSender lmail.EmailProvider, from mail.Address, to []mail.Address, errorLogger *log.Logger) *Report {
return &Report{
appName: appName,
email: emailConfig{
sender: mailSender,
from: from,
to: to,
},
errorLog: errorLogger,
}
}
func (report *Report) Message(subject, body string) {
if body == `` {
body = subject
}
m := fmt.Sprintf("%s\n%s\n\n", time.Now().Format("02.01.2006 15:04:05"), body)
print(m)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := report.email.sender.Send(&lmail.Data{
From: report.email.from,
To: report.email.to,
Subject: subject,
Body: m,
}); err != nil {
if report.errorLog != nil {
report.logError(m)
}
}
}()
if report.errorLog != nil {
wg.Add(1)
go func() {
defer wg.Done()
report.logError(m)
}()
}
wg.Wait()
}
// Для теста. Выводит в stdout sql запрос с встроенными параметрами
func (report *Report) Sql(query string, params ...interface{}) {
var FileWithLineNum = report.FileWithLineNum()
fmt.Printf("\n%s\n%s\n", FileWithLineNum, sqlparams.Inline(query, params...))
}
// Сообщение об ошибке выполнения sql запроса
func (report *Report) SqlError(r interface{}, err error, query string, params ...interface{}) {
var FileWithLineNum = report.FileWithLineNum()
if err == nil {
fmt.Printf("\n%s\n%s\n", FileWithLineNum, sqlparams.Inline(query, params...))
return
}
fmt.Printf("\n%s:\n%s\n%s\n", FileWithLineNum, err.Error(), sqlparams.Inline(query, params...))
m := report.createMessage(r, FileWithLineNum, err, sqlparams.Inline(query, params...))
go func() {
if err := report.email.sender.Send(&lmail.Data{
From: report.email.from,
To: report.email.to,
Subject: `!!! ` + report.appName + `: SQL проблема !!!`,
Body: m,
WithLimiter: true,
}); err != nil {
if report.errorLog != nil {
report.logError(m)
}
}
}()
if report.errorLog != nil {
go report.logError(m)
}
}
func (report *Report) Error(r interface{}, err error) {
var FileWithLineNum = report.FileWithLineNum()
fmt.Printf("\n%s\n%s\n", FileWithLineNum, err.Error())
m := report.createMessage(r, FileWithLineNum, err, "")
go func() {
if err := report.email.sender.Send(&lmail.Data{
From: report.email.from,
To: report.email.to,
Subject: `!!! ` + report.appName + `: ошибка !!!`,
Body: m,
WithLimiter: true,
}); err != nil {
if report.errorLog != nil {
report.logError(m)
}
}
}()
if report.errorLog != nil {
go report.logError(m)
}
}
func (report *Report) createMessage(r interface{}, FileWithLineNum string, err error, sql string) string {
var sb strings.Builder
var errStr string
if err != nil {
errStr = err.Error() + "\n"
} else {
errStr = "\n"
}
if len(sql) > 0 {
fmt.Fprintf(&sb, "%s\n%s\n%s\n%s\n\n%s",
time.Now().Format("02.01.2006 15:04:05"),
FileWithLineNum,
errStr,
sql,
getObjectData(r))
} else {
fmt.Fprintf(&sb, "%s\n%s\n%s\n%s",
time.Now().Format("02.01.2006 15:04:05"),
FileWithLineNum,
errStr,
getObjectData(r))
}
return sb.String()
}
func (report *Report) FileWithLineNum() string {
for i := 2; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
if ok {
return file + ":" + strconv.FormatInt(int64(line), 10)
}
}
return ``
}
func (report *Report) FilesWithLineNum() (out []string) {
for i := 0; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
if ok && strings.Contains(file, `/`+report.appName+`/`) {
out = append(out, file+":"+strconv.FormatInt(int64(line), 10))
}
}
return out
}
func (report *Report) logError(m string) {
report.errorLog.Printf("%s", m)
report.errorLog.Println(strings.Repeat("—", 70))
report.errorLog.Println("")
}
func getObjectData(object interface{}, tabs ...string) string {
if object == nil {
return ``
}
t := reflect.TypeOf(object)
val := reflect.ValueOf(object)
if t.Kind() == reflect.Ptr {
val = val.Elem()
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return "\n"
}
var (
key string
v interface{}
sb strings.Builder
timeType = reflect.TypeOf(time.Time{})
tab = strings.Join(tabs, ``)
handleStruct = func(i int) {
switch {
case t.Field(i).Type == timeType: // time.Time
fmt.Fprintf(&sb, "%s%s: %v\n", tab, t.Field(i).Name, val.Field(i))
case t.Field(i).Type.Kind() == reflect.Ptr && t.Field(i).Type.Elem() == timeType: // *time.Time
fmt.Fprintf(&sb, "%s%s: %v\n", tab, t.Field(i).Name, val.Field(i).Elem())
default: // struct or *struct
fmt.Fprintf(&sb, "%s%s:\n", tab, t.Field(i).Name)
sb.WriteString(getObjectData(val.Field(i).Interface(), tab, "\t"))
}
}
)
for i := 0; i < val.NumField(); i++ {
if !val.Field(i).CanInterface() { // Если поле неэкспортируемое, - проигнорировать
continue
}
switch t.Field(i).Type.Kind() {
case reflect.Ptr:
if val.Field(i).IsNil() {
v = nil
} else if t.Field(i).Type.Elem().Kind() == reflect.Struct {
handleStruct(i)
continue
} else {
v = val.Field(i).Elem().Interface()
}
key = t.Field(i).Name
case reflect.Struct:
handleStruct(i)
continue
default:
key = t.Field(i).Name
v = val.Field(i).Interface()
}
fmt.Fprintf(&sb, "%s%s = %v\n", tab, key, v)
}
sb.WriteByte('\n')
return sb.String()
}