-
Notifications
You must be signed in to change notification settings - Fork 28
/
models.go
146 lines (125 loc) · 2.87 KB
/
models.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package lager
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)
type LogLevel int
const (
DEBUG LogLevel = iota
INFO
ERROR
FATAL
)
var logLevelStr = [...]string{
DEBUG: "debug",
INFO: "info",
ERROR: "error",
FATAL: "fatal",
}
func (l LogLevel) String() string {
if DEBUG <= l && l <= FATAL {
return logLevelStr[l]
}
return "invalid"
}
func LogLevelFromString(s string) (LogLevel, error) {
for k, v := range logLevelStr {
if v == s {
return LogLevel(k), nil
}
}
return -1, fmt.Errorf("invalid log level: %s", s)
}
type Data map[string]interface{}
type rfc3339Time time.Time
const rfc3339Nano = "2006-01-02T15:04:05.000000000Z07:00"
func (t rfc3339Time) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf(`"%s"`, time.Time(t).UTC().Format(rfc3339Nano))
return []byte(stamp), nil
}
func (t *rfc3339Time) UnmarshalJSON(data []byte) error {
return (*time.Time)(t).UnmarshalJSON(data)
}
type LogFormat struct {
Timestamp string `json:"timestamp"`
Source string `json:"source"`
Message string `json:"message"`
LogLevel LogLevel `json:"log_level"`
Data Data `json:"data"`
Error error `json:"-"`
time time.Time
}
func (log LogFormat) ToJSON() []byte {
content, err := json.Marshal(log)
if err != nil {
log.Data = dataForJSONMarhallingError(err, log.Data)
content, err = json.Marshal(log)
if err != nil {
panic(err)
}
}
return content
}
func (log LogFormat) toPrettyJSON() []byte {
t := log.time
if t.IsZero() {
t = parseTimestamp(log.Timestamp)
}
prettyLog := struct {
Timestamp rfc3339Time `json:"timestamp"`
Level string `json:"level"`
Source string `json:"source"`
Message string `json:"message"`
Data Data `json:"data"`
Error error `json:"-"`
}{
Timestamp: rfc3339Time(t),
Level: log.LogLevel.String(),
Source: log.Source,
Message: log.Message,
Data: log.Data,
Error: log.Error,
}
content, err := json.Marshal(prettyLog)
if err != nil {
prettyLog.Data = dataForJSONMarhallingError(err, prettyLog.Data)
content, err = json.Marshal(prettyLog)
if err != nil {
panic(err)
}
}
return content
}
func dataForJSONMarhallingError(err error, data Data) Data {
_, ok1 := err.(*json.UnsupportedTypeError)
_, ok2 := err.(*json.MarshalerError)
errKey := "unknown_error"
if ok1 || ok2 {
errKey = "lager serialisation error"
}
return map[string]interface{}{
errKey: err.Error(),
"data_dump": fmt.Sprintf("%#v", data),
}
}
func parseTimestamp(s string) time.Time {
if s == "" {
return time.Now()
}
n := strings.IndexByte(s, '.')
if n <= 0 || n == len(s)-1 {
return time.Now()
}
sec, err := strconv.ParseInt(s[:n], 10, 64)
if err != nil || sec < 0 {
return time.Now()
}
nsec, err := strconv.ParseInt(s[n+1:], 10, 64)
if err != nil || nsec < 0 {
return time.Now()
}
return time.Unix(sec, nsec)
}