-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_content.go
88 lines (77 loc) · 1.43 KB
/
log_content.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
package logx
import (
"bytes"
"encoding/json"
)
// LogFormat logformat enum
type (
DateFormat int
FileFormat int
)
const (
LongDateFormat DateFormat = iota
ShortDateFormat
)
const (
LongFileFormat FileFormat = iota
ShortFileFormat
)
type LogOptions struct {
logFormat LogFormat
dateFormat DateFormat
fileFormat FileFormat
}
type LogContent struct {
Prefix string `json:"prefix"`
Time string `json:"time"`
Level string `json:"level"`
File string `json:"file"`
Msg string `json:"msg"`
Color bool `json:"-"`
LevelInt int `json:"-"`
}
// Json LogContent to json
// returns []byte
func (cc *LogContent) Json() []byte {
if cc == nil {
return []byte("")
}
cc.Level = levels[cc.LevelInt]
var s bytes.Buffer
b, _ := json.Marshal(&cc)
if cc.Color {
s.WriteString(logColor[cc.LevelInt])
}
s.Write(b)
if cc.Color {
s.WriteString(endColor)
}
return s.Bytes()
}
// Text LogContent to Text
// returns []byte
func (cc *LogContent) Text() []byte {
var s bytes.Buffer
if cc.Color {
s.WriteString(logColor[cc.LevelInt])
}
if cc.Prefix != "" {
s.WriteString("[")
s.WriteString(cc.Prefix)
s.WriteString("]")
s.WriteByte(' ')
}
s.WriteString(cc.Time)
s.WriteByte(' ')
s.WriteString("[")
s.WriteString(levels[cc.LevelInt])
s.WriteString("]")
s.WriteByte(' ')
s.WriteString(cc.File)
s.WriteString(": ")
s.WriteString(cc.Msg)
if cc.Color {
s.WriteString(endColor)
}
return s.Bytes()
}