Skip to content

Commit

Permalink
tweak formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Jul 25, 2022
1 parent de5d25d commit 5520267
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
// FormatterArgs is a parsed sturct from json input
type FormatterArgs struct {
Time string // "2019-07-10T05:35:54.277Z"
Message string // "a structure message"
Level string // "info"
Caller string // "prog.go:42"
Goid string // "123"
Stack string // "<stack string>"
Message string // "a structure message"
KeyValues []struct {
Key string // "foo"
Value string // "bar"
Expand All @@ -38,15 +38,15 @@ func formatterArgsPos(key string) (pos int) {
switch key {
case "time":
pos = 1
case "message", "msg":
pos = 2
case "level":
pos = 3
pos = 2
case "caller":
pos = 4
pos = 3
case "goid":
pos = 5
pos = 4
case "stack":
pos = 5
case "message", "msg":
pos = 6
}
return
Expand Down Expand Up @@ -104,7 +104,9 @@ func parseFormatterArgs(json []byte, args *FormatterArgs) {
if pos == 2 && len(str) != 0 && str[len(str)-1] == '\n' {
str = str[:len(str)-1]
}
slice[pos-1] = b2s(str)
if slice[pos-1] == "" {
slice[pos-1] = b2s(str)
}
} else {
args.KeyValues = append(args.KeyValues, struct {
Key, Value string
Expand Down
2 changes: 1 addition & 1 deletion formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestFormatterParse(t *testing.T) {
var jsons = []string{
`{"time":"2019-07-10T05:35:54.277Z","level":"info","caller":"pretty.go:42","error":"这是一个🌐哦\n","foo":"bar","n":42,"t":true,"f":false,"o":null,"a":[1,2,3],"obj":{"a":[1,2], "b":{"c":3}},"message":"hello json console color writer\t123"}`,
`{"time":"2019-07-10T05:35:54.277Z","level":"debug","level":"error","caller":"pretty.go:42","error":"这是一个🌐哦\n","foo":"bar","n":42,"t":true,"f":false,"o":null,"a":[1,2,3],"obj":{"a":[1,2], "b":{"c":3}},"message":"hello json console color writer\t123"}`,
`{"ts":1234567890,"level":"info","caller":"pretty.go:42","foo":"haha","message":"hello self-define time field\t\n"}`,
}

Expand Down

0 comments on commit 5520267

Please sign in to comment.