-
Notifications
You must be signed in to change notification settings - Fork 6
/
parser_line.go
60 lines (54 loc) · 1.28 KB
/
parser_line.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
package xlog
import (
"time"
"get.pme.sh/pmesh/util"
"github.com/rs/zerolog"
"github.com/valyala/fastjson"
)
type Line struct {
*fastjson.Value
Raw []byte
}
func ParseLine(line []byte) (ll Line, err error) {
v, err := fastjson.ParseBytes(line)
if err != nil {
return
}
return Line{v, line}, nil
}
func (ll Line) Domain() string {
sb := ll.GetStringBytes(zerolog.CallerFieldName)
return util.UnsafeString(sb)
}
func (ll Line) Time() time.Time {
f := ll.GetInt64(zerolog.TimestampFieldName)
if f == 0 {
return time.Time{}
}
return time.UnixMilli(f)
}
func (ll Line) Level() Level {
level := ll.Get(zerolog.LevelFieldName)
if level != nil {
if level.Type() == fastjson.TypeString {
lstr := level.GetStringBytes()
if v, ok := unformatLevel[util.UnsafeString(lstr)]; ok {
return v
}
} else if level.Type() == fastjson.TypeNumber {
return Level(level.GetInt())
}
}
return LevelNone
}
var unformatLevel = map[string]Level{
zerolog.LevelTraceValue: LevelTrace,
zerolog.LevelDebugValue: LevelDebug,
zerolog.LevelInfoValue: LevelInfo,
zerolog.LevelWarnValue: LevelWarn,
zerolog.LevelErrorValue: LevelError,
zerolog.LevelFatalValue: LevelFatal,
zerolog.LevelPanicValue: LevelPanic,
"disabled": LevelSuppress,
"": LevelNone,
}