-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
56 lines (47 loc) · 1.13 KB
/
message.go
File metadata and controls
56 lines (47 loc) · 1.13 KB
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
/*
* Copyright (c) 2024-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package logx
import (
"time"
"go.osspkg.com/ioutils/pool"
)
//go:generate easyjson
var poolMessage = pool.New[*Message](func() *Message {
return newMessage()
})
//easyjson:json
type Message struct {
Time time.Time `json:"time" yaml:"time"`
Level string `json:"level" yaml:"level"`
Message string `json:"msg" yaml:"msg"`
Ctx []interface{} `json:"-"`
Map map[string]string `json:"ctx,omitempty" yaml:"ctx,omitempty,inline"`
}
func newMessage() *Message {
return &Message{
Ctx: make([]interface{}, 0, 10),
Map: make(map[string]string, 10),
}
}
func (v *Message) Reset() {
v.Ctx = v.Ctx[:0]
for k := range v.Map {
delete(v.Map, k)
}
}
func (v *Message) CtxToMap() {
count := len(v.Ctx)
if count == 0 {
return
}
if count%2 != 0 {
v.Ctx = append(v.Ctx, nil)
count++
}
for i := 0; i < count; i = i + 2 {
v.Map[typing(v.Ctx[i])] = typing(v.Ctx[i+1])
}
v.Ctx = v.Ctx[:0]
}