forked from AlexStocks/log4go
/
logrecord.go
187 lines (171 loc) · 4.13 KB
/
logrecord.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/******************************************************
# DESC :
# AUTHOR : Alex Stocks
# VERSION : 1.0
# LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com
# MOD : 2018-03-27 14:48
# FILE : logrecord.go
******************************************************/
package log4go
import (
"bytes"
"encoding/json"
"strconv"
"time"
)
import (
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)
// suppress unused package warning
var (
_ *json.RawMessage
_ *jlexer.Lexer
_ *jwriter.Writer
_ easyjson.Marshaler
)
/****** LogRecord ******/
// A LogRecord contains all of the pertinent information for each message
type LogRecord struct {
Level Level `json:"level,omitempty"` // The log level
Created time.Time `json:"timestamp,omitempty"` // The time at which the log message was created (nanoseconds)
Source string `json:"source,omitempty"` // The message source
Message string `json:"log,omitempty"` // The log message
}
// 不要尝试使用json.Marshal,这个函数使用了反射,效率很低,详细结果见测试
// func (r LogRecord) MarshalJSON() ([]byte, error) {
func (r LogRecord) JSON() []byte {
var buf bytes.Buffer
buf.WriteString("{\"level\":")
buf.WriteString(strconv.Itoa(int(r.Level)))
//buf.WriteString(",")
//
//buf.WriteString("\"timestamp\":")
buf.WriteString(",\"timestamp\":")
timeJson, _ := r.Created.MarshalJSON()
buf.Write(timeJson)
//buf.WriteString(",")
//
//buf.WriteString("\"source\":")
//buf.WriteString("\"")
buf.WriteString(",\"source\":\"")
buf.WriteString(r.Source)
//buf.WriteString("\"")
//buf.WriteString(",")
//
//buf.WriteString("\"log\":")
//buf.WriteString("\"")
buf.WriteString("\",\"log\":\"")
buf.WriteString(r.Message)
//buf.WriteString("\"")
//buf.WriteString("}")
buf.WriteString("\"}")
return buf.Bytes()
}
func easyjson15d5d517Decodeg(in *jlexer.Lexer, out *LogRecord) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "level":
out.Level = Level(in.Int())
case "timestamp":
if data := in.Raw(); in.Ok() {
in.AddError((out.Created).UnmarshalJSON(data))
}
case "source":
out.Source = string(in.String())
case "log":
out.Message = string(in.String())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjson15d5d517Encodeg(out *jwriter.Writer, in LogRecord) {
out.RawByte('{')
first := true
_ = first
if in.Level != 0 {
const prefix string = ",\"level\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Level))
}
if true {
const prefix string = ",\"timestamp\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Raw((in.Created).MarshalJSON())
}
if in.Source != "" {
const prefix string = ",\"source\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Source))
}
if in.Message != "" {
const prefix string = ",\"log\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Message))
}
out.RawByte('}')
}
// MarshalJSON supports json.Marshaler interface
func (v LogRecord) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
easyjson15d5d517Encodeg(&w, v)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (v LogRecord) MarshalEasyJSON(w *jwriter.Writer) {
easyjson15d5d517Encodeg(w, v)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (v *LogRecord) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
easyjson15d5d517Decodeg(&r, v)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *LogRecord) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjson15d5d517Decodeg(l, v)
}