-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_json.go
53 lines (45 loc) · 986 Bytes
/
core_json.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
package log
import (
"encoding/json"
"io"
"strings"
"unsafe"
)
type JsonEncoder struct {
}
type structureLog struct {
Level Level `json:"level"`
Date string `json:"date,omitempty"`
Caller string `json:"caller,omitempty"`
Params interface{} `json:"params,omitempty"`
Fields []string `json:"fields,omitempty"`
Stack []string `json:"stack,omitempty"`
}
func NewJsonCore(writer io.Writer) *Core {
return &Core{
allWriter: writer,
encoder: new(JsonEncoder),
}
}
func (e *JsonEncoder) Encode(entry *Entry, params []any) string {
if entry == nil {
return ""
}
if params != nil {
format, ok := params[0].(string)
if ok && strings.ContainsRune(format, '%') {
params = params[1:]
}
}
buf, err := json.Marshal(&structureLog{
Date: entry.Date,
Level: entry.Level,
Caller: entry.Caller,
Fields: entry.Fields,
Params: params,
})
if err != nil {
return ""
}
return unsafe.String(unsafe.SliceData(buf), len(buf))
}