-
Notifications
You must be signed in to change notification settings - Fork 41
/
loggingutil.go
207 lines (180 loc) · 5.94 KB
/
loggingutil.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
/*
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package p2putil
import (
"fmt"
"net"
"github.com/aergoio/aergo-lib/log"
"github.com/aergoio/aergo/v2/p2p/p2pcommon"
"github.com/aergoio/aergo/v2/types"
"github.com/rs/zerolog"
)
// Deprecated
func DebugLogReceiveMsg(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, peer p2pcommon.RemotePeer, additional interface{}) {
if additional != nil {
logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str("from_peer", peer.Name()).Str("other", fmt.Sprint(additional)).
Msg("Received a message")
} else {
logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str("from_peer", peer.Name()).
Msg("Received a message")
}
}
// Deprecated
func DebugLogReceiveResponseMsg(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, reqID string, peer p2pcommon.RemotePeer, additional interface{}) {
if additional != nil {
logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str(LogOrgReqID, reqID).Str("from_peer", peer.Name()).Str("other", fmt.Sprint(additional)).
Msg("Received a response message")
} else {
logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str(LogOrgReqID, reqID).Str("from_peer", peer.Name()).
Msg("Received a response message")
}
}
// DebugLogReceive log received remote message with debug level.
func DebugLogReceive(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, peer p2pcommon.RemotePeer, additional zerolog.LogObjectMarshaler) {
if additional != nil {
logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str("from_peer", peer.Name()).Object("body", additional).Msg("Received a message")
} else {
logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str("from_peer", peer.Name()).
Msg("Received a message")
}
}
// DebugLogReceive log received remote response message with debug level.
func DebugLogReceiveResponse(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, reqID string, peer p2pcommon.RemotePeer, additional zerolog.LogObjectMarshaler) {
if additional != nil {
logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str(LogOrgReqID, reqID).Str("from_peer", peer.Name()).Object("body", additional).
Msg("Received a response message")
} else {
logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str(LogOrgReqID, reqID).Str("from_peer", peer.Name()).
Msg("Received a response message")
}
}
type LogStringersMarshaller struct {
arr []fmt.Stringer
limit int
}
func NewLogStringersMarshaller(arr []fmt.Stringer, limit int) *LogStringersMarshaller {
return &LogStringersMarshaller{arr: arr, limit: limit}
}
func (m *LogStringersMarshaller) MarshalZerologArray(a *zerolog.Array) {
size := len(m.arr)
if size > m.limit {
for i := 0; i < m.limit-1; i++ {
a.Str(m.arr[i].String())
}
a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1))
} else {
for _, element := range m.arr {
a.Str(element.String())
}
}
}
type LogPeerMetasMarshaller struct {
metas []p2pcommon.PeerMeta
limit int
}
func NewLogPeerMetasMarshaller(metas []p2pcommon.PeerMeta, limit int) *LogPeerMetasMarshaller {
return &LogPeerMetasMarshaller{metas: metas, limit: limit}
}
func (m *LogPeerMetasMarshaller) MarshalZerologArray(a *zerolog.Array) {
size := len(m.metas)
if size > m.limit {
size = m.limit - 1
for _, meta := range m.metas[:size] {
a.Str(ShortMetaForm(meta))
}
a.Str(fmt.Sprintf("(and %d more)", len(m.metas)-size))
} else {
for _, meta := range m.metas {
a.Str(ShortMetaForm(meta))
}
}
}
type LogPeersMarshaller struct {
metas []p2pcommon.RemotePeer
limit int
}
func NewLogPeersMarshaller(metas []p2pcommon.RemotePeer, limit int) *LogPeersMarshaller {
return &LogPeersMarshaller{metas: metas, limit: limit}
}
func (m *LogPeersMarshaller) MarshalZerologArray(a *zerolog.Array) {
size := len(m.metas)
if size > m.limit {
for i := 0; i < m.limit-1; i++ {
a.Str(m.metas[i].Name())
}
a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1))
} else {
for _, meta := range m.metas {
a.Str(meta.Name())
}
}
}
type LogStringsMarshaller struct {
strs []string
limit int
}
func NewLogStringsMarshaller(strs []string, limit int) *LogStringsMarshaller {
return &LogStringsMarshaller{strs: strs, limit: limit}
}
func (m *LogStringsMarshaller) MarshalZerologArray(a *zerolog.Array) {
size := len(m.strs)
if size > m.limit {
for i := 0; i < m.limit-1; i++ {
a.Str(m.strs[i])
}
a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1))
} else {
for _, meta := range m.strs {
a.Str(meta)
}
}
}
type LogPeerIdsMarshaller struct {
arr []types.PeerID
limit int
}
func NewLogPeerIdsMarshaller(arr []types.PeerID, limit int) *LogPeerIdsMarshaller {
return &LogPeerIdsMarshaller{arr: arr, limit: limit}
}
func (m LogPeerIdsMarshaller) MarshalZerologArray(a *zerolog.Array) {
size := len(m.arr)
if size > m.limit {
for i := 0; i < m.limit-1; i++ {
a.Str(ShortForm(m.arr[i]))
}
a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1))
} else {
for _, element := range m.arr {
a.Str(ShortForm(element))
}
}
}
type LogIPNetMarshaller struct {
arr []*net.IPNet
limit int
}
func NewLogIPNetMarshaller(arr []*net.IPNet, limit int) *LogIPNetMarshaller {
return &LogIPNetMarshaller{arr: arr, limit: limit}
}
func (m LogIPNetMarshaller) MarshalZerologArray(a *zerolog.Array) {
size := len(m.arr)
if size > m.limit {
for i := 0; i < m.limit-1; i++ {
a.Str(m.arr[i].String())
}
a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1))
} else {
for _, element := range m.arr {
a.Str(element.String())
}
}
}
type AgentCertMarshaller struct {
*p2pcommon.AgentCertificateV1
}
func (a AgentCertMarshaller) MarshalZerologObject(e *zerolog.Event) {
e.Str("issuer", a.BPID.Pretty()).Str("agent", a.AgentID.Pretty()).Array("addrs", NewLogStringsMarshaller(a.AgentAddress, 10)).
Time("created", a.CreateTime).Time("expire", a.ExpireTime).Uint32("version", a.Version)
}