-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.go
139 lines (115 loc) · 2.93 KB
/
general.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
package formatter
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
"github.com/wesleysnt/framework/contracts/config"
"github.com/wesleysnt/framework/support/json"
)
type General struct {
config config.Config
}
func NewGeneral(config config.Config) *General {
return &General{
config: config,
}
}
func (general *General) Format(entry *logrus.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
cstSh, err := time.LoadLocation(general.config.GetString("app.timezone"))
if err != nil {
return nil, err
}
timestamp := entry.Time.In(cstSh).Format("2006-01-02 15:04:05")
b.WriteString(fmt.Sprintf("[%s] %s.%s: %s\n", timestamp, general.config.GetString("app.env"), entry.Level, entry.Message))
data := entry.Data
if len(data) > 0 {
formattedData, err := formatData(data)
if err != nil {
return nil, err
}
b.WriteString(formattedData)
}
return b.Bytes(), nil
}
func formatData(data logrus.Fields) (string, error) {
var builder strings.Builder
if len(data) > 0 {
removedData := deleteKey(data, "root")
if len(removedData) > 0 {
removedDataBytes, err := json.Marshal(removedData)
if err != nil {
return "", err
}
builder.WriteString(fmt.Sprintf("fields: %s\n", string(removedDataBytes)))
}
root, err := cast.ToStringMapE(data["root"])
if err != nil {
return "", err
}
for _, key := range []string{"code", "context", "domain", "hint", "owner", "request", "response", "tags", "user"} {
if value, exists := root[key]; exists && value != nil {
v, err := json.Marshal(value)
if err != nil {
return "", err
}
builder.WriteString(fmt.Sprintf(`%s: %v"\n`, key, string(v)))
}
}
if stackTraceValue, exists := root["stacktrace"]; exists && stackTraceValue != nil {
traces, err := formatStackTraces(stackTraceValue)
if err != nil {
return "", err
}
builder.WriteString(traces)
}
}
return builder.String(), nil
}
func deleteKey(data logrus.Fields, keyToDelete string) logrus.Fields {
dataCopy := make(logrus.Fields)
for key, value := range data {
if key != keyToDelete {
dataCopy[key] = value
}
}
return dataCopy
}
type StackTrace struct {
Root struct {
Message string `json:"message"`
Stack []string `json:"stack"`
} `json:"root"`
Wrap []struct {
Message string `json:"message"`
Stack string `json:"stack"`
} `json:"wrap"`
}
func formatStackTraces(stackTraces any) (string, error) {
var formattedTraces strings.Builder
data, err := json.Marshal(stackTraces)
if err != nil {
return "", err
}
var traces StackTrace
err = json.Unmarshal(data, &traces)
if err != nil {
return "", err
}
formattedTraces.WriteString("trace:\n")
root := traces.Root
if len(root.Stack) > 0 {
for _, stackStr := range root.Stack {
formattedTraces.WriteString(fmt.Sprintf("\t%s\n", stackStr))
}
}
return formattedTraces.String(), nil
}