-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobserver.go
167 lines (135 loc) · 3.57 KB
/
observer.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
// Package ctxz implements wrapper to report aggregated messages for github.com/bool64/ctxd.Logger.
package ctxz
import (
"bytes"
"context"
"encoding"
"encoding/json"
"errors"
"fmt"
"github.com/bool64/ctxd"
"github.com/bool64/logz"
)
// Observer keeps track of logged messages.
type Observer struct {
debug *logz.Observer
info *logz.Observer
important *logz.Observer
warn *logz.Observer
error *logz.Observer
logger ctxd.Logger
}
type tuples struct {
ctx context.Context
kv []interface{}
}
func (t tuples) MarshalJSON() ([]byte, error) { //nolint:funlen,cyclop
kv := t.kv[0:len(t.kv):len(t.kv)]
ctxFields := ctxd.Fields(t.ctx)
if len(ctxFields) > 0 {
kv = append(kv, ctxFields...)
}
m := make(map[string]interface{}, len(kv))
var (
label string
ok bool
err error
)
for i, l := range kv {
if label == "" {
label, ok = l.(string)
if !ok {
m["malformedFields"] = kv[i:]
break
}
} else {
switch v := l.(type) {
case error:
l = v.Error()
var se ctxd.StructuredError
if errors.As(v, &se) {
for k, v := range se.Fields() {
m[k] = v
}
}
case json.Marshaler:
case encoding.TextMarshaler:
l, err = v.MarshalText()
if err != nil {
return nil, err
}
case fmt.Stringer:
l = v.String()
}
m[label] = l
label = ""
}
}
b := bytes.Buffer{}
e := json.NewEncoder(&b)
e.SetEscapeHTML(false)
err = e.Encode(m)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
// Debug logs debug message.
func (o Observer) Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
o.debug.ObserveMessage(msg, tuples{ctx: ctx, kv: keysAndValues})
o.logger.Debug(ctx, msg, keysAndValues...)
}
// Info logs informational message.
func (o Observer) Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
o.info.ObserveMessage(msg, tuples{ctx: ctx, kv: keysAndValues})
o.logger.Info(ctx, msg, keysAndValues...)
}
// Important logs important information.
func (o Observer) Important(ctx context.Context, msg string, keysAndValues ...interface{}) {
o.important.ObserveMessage(msg, tuples{ctx: ctx, kv: keysAndValues})
o.logger.Important(ctx, msg, keysAndValues...)
}
// Warn logs a warning.
func (o Observer) Warn(ctx context.Context, msg string, keysAndValues ...interface{}) {
o.warn.ObserveMessage(msg, tuples{ctx: ctx, kv: keysAndValues})
o.logger.Warn(ctx, msg, keysAndValues...)
}
// Error logs an error.
func (o Observer) Error(ctx context.Context, msg string, keysAndValues ...interface{}) {
o.error.ObserveMessage(msg, tuples{ctx: ctx, kv: keysAndValues})
o.logger.Error(ctx, msg, keysAndValues...)
}
// LevelObservers returns .
func (o Observer) LevelObservers() []*logz.Observer {
return []*logz.Observer{o.debug, o.info, o.important, o.warn, o.error}
}
// WithLogger returns a copy of Observer with logger, level buckets remain the same.
func (o Observer) WithLogger(l ctxd.Logger) Observer {
o.logger = l
return o
}
// CtxdLogger is a service provider.
func (o Observer) CtxdLogger() ctxd.Logger {
return o
}
// NewObserver initializes Observer instance.
func NewObserver(logger ctxd.Logger, conf ...logz.Config) Observer {
o := Observer{
logger: logger,
}
cfg := logz.Config{}
if len(conf) == 1 {
cfg = conf[0]
}
cfg.Name = "Debug"
o.debug = &logz.Observer{Config: cfg}
cfg.Name = "Info"
o.info = &logz.Observer{Config: cfg}
cfg.Name = "Important"
o.important = &logz.Observer{Config: cfg}
cfg.Name = "Warning"
o.warn = &logz.Observer{Config: cfg}
cfg.Name = "Error"
o.error = &logz.Observer{Config: cfg}
return o
}