-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathline.go
70 lines (63 loc) · 1.4 KB
/
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
61
62
63
64
65
66
67
68
69
70
package vlog
import (
"encoding/json"
"fmt"
"time"
"github.com/fatih/color"
)
type Line struct {
color color.Attribute
now time.Time
tag Tag
msg string
fields []KeyValue
}
func (l *Line) ToFormat(format LogFormat) string {
switch format {
case LogFormatText:
return l.ToText()
case LogFormatJson:
j, _ := l.ToJson()
return j
case LogFormatColoredText:
return l.ToColoredText()
default:
return l.ToText()
}
}
func (l *Line) ToColoredText() string {
msg := fmt.Sprintf("%s %s",
color.New(color.FgHiWhite).Sprintf(l.now.Format(time.DateTime)),
color.New(l.color).Sprint(l.tag),
)
msg += color.New(l.color).Sprintf(" msg=")
msg += fmt.Sprint(l.msg)
for _, field := range l.fields {
msg += color.New(l.color).Sprintf(" %s=", field.Key)
msg += fmt.Sprint(field.Value)
}
return msg
}
func (l *Line) ToText() string {
msg := fmt.Sprintf("%s %s msg=%s", l.now.Format(time.DateTime), l.tag, l.msg)
for _, field := range l.fields {
msg += fmt.Sprintf(" %s=%s", field.Key, field.Value)
}
return msg
}
func (l *Line) ToJson() (string, error) {
m := map[string]any{
"seconds": l.now.Unix(),
"nanoseconds": l.now.UnixNano(),
"kind": l.tag,
"msg": l.msg,
}
for _, field := range l.fields {
m[field.Key] = field.Value
}
j, err := json.Marshal(m)
if err != nil {
return "", fmt.Errorf("failed to marshal json: %w", err)
}
return string(j), nil
}