Skip to content

Commit 4975f8e

Browse files
committed
feat: log support json format
1 parent ffb8ea4 commit 4975f8e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

log/formatter/json_formatter.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package formatter
2+
3+
import (
4+
"encoding/json"
5+
"github.com/forgoer/thinkgo/log/record"
6+
)
7+
8+
type JsonFormatter struct {
9+
}
10+
11+
func NewJsonFormatter() *JsonFormatter {
12+
j := &JsonFormatter{}
13+
return j
14+
}
15+
16+
func (f *JsonFormatter) Format(r record.Record) string {
17+
normalized := make(map[string]interface{})
18+
19+
normalized["message"] = r.Message
20+
normalized["level"] = r.Level
21+
normalized["level_name"] = r.LevelName
22+
normalized["channel"] = r.Channel
23+
normalized["datetime"] = r.Datetime.Local().Format("2006-01-02 15:04:05.000")
24+
25+
output, _ := json.Marshal(normalized)
26+
return string(output) + "\n"
27+
}
28+
29+
func (f *JsonFormatter) FormatBatch(rs []record.Record) string {
30+
message := ""
31+
for _, r := range rs {
32+
message = message + f.Format(r)
33+
}
34+
return message
35+
}

0 commit comments

Comments
 (0)