-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry_node.go
106 lines (91 loc) · 2.14 KB
/
entry_node.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
package tower
import (
"bytes"
"context"
"encoding/json"
"time"
)
var _ Entry = (*EntryNode)(nil)
// EntryNode is the default implementation of Entry for tower.
type EntryNode struct {
inner *entryBuilder
}
// MarshalJSON implements the json.Marshaler interface.
func (e EntryNode) MarshalJSON() ([]byte, error) {
b := &bytes.Buffer{}
enc := json.NewEncoder(b)
enc.SetEscapeHTML(false)
var ctx any
v := e.Context()
if len(v) == 1 {
ctx = v[0]
} else {
ctx = v
}
err := enc.Encode(implJsonMarshaler{
Time: e.Time().Format(time.RFC3339),
Code: e.Code(),
Message: e.Message(),
Caller: e.Caller(),
Key: e.Key(),
Level: e.Level().String(),
Service: &e.inner.tower.service,
Context: ctx,
})
return b.Bytes(), err
}
// Code returns the original code of the type.
func (e EntryNode) Code() int {
return e.inner.code
}
// Time returns the time of the entry.
func (e EntryNode) Time() time.Time {
return e.inner.time
}
// Service returns the service name of the entry.
func (e EntryNode) Service() Service {
return e.inner.tower.GetService()
}
// HTTPCode return HTTP Status Code for the type.
func (e EntryNode) HTTPCode() int {
switch {
case e.inner.code >= 200 && e.inner.code <= 599:
return e.inner.code
case e.inner.code > 999:
code := e.inner.code % 1000
if code >= 200 && code <= 599 {
return code
}
}
return 200
}
// Message returns the message.
func (e EntryNode) Message() string {
return e.inner.message
}
// Key returns the key of the entry.
func (e EntryNode) Key() string {
return e.inner.key
}
// Caller returns the tower.Caller of the entry.
func (e EntryNode) Caller() Caller {
return e.inner.caller
}
// Context returns the context of the entry.
func (e EntryNode) Context() []any {
return e.inner.context
}
// Level Gets the level of this message.
func (e EntryNode) Level() Level {
return e.inner.level
}
// Log logs the entry.
func (e EntryNode) Log(ctx context.Context) Entry {
e.inner.tower.Log(ctx, e)
return e
}
// Notify sends the entry to the messengers.
func (e EntryNode) Notify(ctx context.Context, opts ...MessageOption) Entry {
e.inner.tower.Notify(ctx, e, opts...)
return e
}