/
trace.go
55 lines (47 loc) · 903 Bytes
/
trace.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
package uhttp
import "github.com/msw-x/moon/ulog"
type Tracer struct {
log *ulog.Log
format Format
formatError Format
validate func(Responce) bool
filter func(Responce) bool
}
func NewTracer(log *ulog.Log) *Tracer {
o := new(Tracer)
o.log = log
return o
}
func (o *Tracer) WithFormat(f Format) *Tracer {
o.format = f
o.formatError = f
return o
}
func (o *Tracer) WithFormatError(f Format) *Tracer {
o.formatError = f
return o
}
func (o *Tracer) WithValidate(f func(Responce) bool) *Tracer {
o.validate = f
return o
}
func (o *Tracer) WithFilter(f func(Responce) bool) *Tracer {
o.filter = f
return o
}
func (o *Tracer) Trace(r Responce) {
if o.filter != nil {
if !o.filter(r) {
return
}
}
ok := r.Ok()
if o.validate != nil {
ok = o.validate(r)
}
if ok {
o.log.Debug(r.Format(o.format))
} else {
o.log.Error(r.Format(o.formatError))
}
}