forked from bketelsen/genkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
94 lines (79 loc) · 1.98 KB
/
logging.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
package main
import "html/template"
var loggingTemplate = template.Must(template.New("render").Parse(`// generated by genkit -- DO NOT EDIT
package {{.Package}}
import (
"time"
"github.com/Sirupsen/logrus"
)
{{range .Types}}
func loggingMiddleware(logger *logrus.Entry) ServiceMiddleware {
return func(next {{.Name}}Service) {{.Name}}Service {
return logmw{logger, next}
}
}
type logmw struct {
logger *logrus.Entry
{{.Name}}Service
}
func (mw logmw) Create(c {{.Name}}) (id string, err error) {
defer func(begin time.Time) {
mw.logger.WithFields(logrus.Fields{
"method": "create",
"{{.LowerName}}": c,
"id": id,
"err": err,
"took": time.Since(begin),
}).Info("request completed")
}(time.Now())
id, err = mw.{{.Name}}Service.Create(c)
return
}
func (mw logmw) Get(id string) (m {{.Name}}, err error) {
defer func(begin time.Time) {
mw.logger.WithFields(logrus.Fields{
"method": "get",
"{{.Name}}": m,
"id": id,
"err": err,
"took": time.Since(begin),
}).Info("request completed")
}(time.Now())
m, err = mw.{{.Name}}Service.Get(id)
return
}
func (mw logmw) Update(m {{.Name}}) (err error) {
defer func(begin time.Time) {
mw.logger.WithFields(logrus.Fields{
"method": "update",
"{{.Name}}": m,
"err": err,
"took": time.Since(begin),
}).Info("request completed")
}(time.Now())
err = mw.{{.Name}}Service.Update(m)
return
}
func (mw logmw) List() (list []{{.Name}}, err error) {
defer func(begin time.Time) {
mw.logger.WithFields(logrus.Fields{
"method": "list",
"err": err,
"took": time.Since(begin),
}).Info("request completed")
}(time.Now())
list, err = mw.{{.Name}}Service.List()
return
}
func (mw logmw) Delete(id string) (err error) {
defer func(begin time.Time) {
mw.logger.WithFields(logrus.Fields{
"method": "delete",
"id": id,
"err": err,
"took": time.Since(begin),
}).Info("request completed")
}(time.Now())
err = mw.{{.Name}}Service.Delete(id)
return
}{{end}}`))