-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
229 lines (193 loc) · 5.44 KB
/
entry.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package log
/*
Copyright 2019 Bruno Moura <brunotm@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"runtime"
"strconv"
"strings"
"time"
)
// Entry is a structured log entry. A entry is not safe for concurrent use.
// A entry must be logged by calling Log(), and cannot be reused after.
type Entry struct {
o Object
l *Logger
level Level
}
// Write logs the current entry. An entry must not be used after calling Write().
func (e Entry) Write() {
if e.o.enc != nil {
if e.o.enc.format == FormatJSON {
e.o.enc.closeObject()
}
e.l.write(e)
}
}
// Level returns the log level of current entry.
func (e Entry) Level() (level Level) {
return e.level
}
// Bytes return the current entry bytes. This is intended to be used in hooks
// That will be applied after calling Log().
// The returned []byte is not a copy and must not be modified directly.
func (e Entry) Bytes() (data []byte) {
return e.o.enc.data
}
// Bool adds the given bool key/value
func (e Entry) Bool(key string, value bool) (entry Entry) {
if e.o.enc != nil {
e.o.Bool(key, value)
}
return e
}
// Float32 adds the given float32 key/value
func (e Entry) Float32(key string, value float32) (entry Entry) {
e.Float64(key, float64(value))
return e
}
// Float64 adds the given float64 key/value
func (e Entry) Float64(key string, value float64) (entry Entry) {
if e.o.enc != nil {
e.o.Float64(key, value)
}
return e
}
// Int8 adds the given int8 key/value
func (e Entry) Int8(key string, value int8) (entry Entry) {
e.Int64(key, int64(value))
return e
}
// Int16 adds the given int16 key/value
func (e Entry) Int16(key string, value int16) (entry Entry) {
e.Int64(key, int64(value))
return e
}
// Int32 adds the given int32 key/value
func (e Entry) Int32(key string, value int32) (entry Entry) {
e.Int64(key, int64(value))
return e
}
// Int adds the given int key/value
func (e Entry) Int(key string, value int) (entry Entry) {
e.Int64(key, int64(value))
return e
}
// Int64 adds the given int64 key/value
func (e Entry) Int64(key string, value int64) (entry Entry) {
if e.o.enc != nil {
e.o.Int64(key, value)
}
return e
}
// Uint8 adds the given uint8 key/value
func (e Entry) Uint8(key string, value uint8) (entry Entry) {
e.Uint64(key, uint64(value))
return e
}
// Uint16 adds the given uint16 key/value
func (e Entry) Uint16(key string, value uint16) (entry Entry) {
e.Uint64(key, uint64(value))
return e
}
// Uint32 adds the given uint32 key/value
func (e Entry) Uint32(key string, value uint32) (entry Entry) {
e.Uint64(key, uint64(value))
return e
}
// Uint adds the given uint16 key/value
func (e Entry) Uint(key string, value uint) (entry Entry) {
e.Uint64(key, uint64(value))
return e
}
// Uint64 adds the given uint key/value
func (e Entry) Uint64(key string, value uint64) (entry Entry) {
if e.o.enc != nil {
e.o.Uint64(key, value)
}
return e
}
// String adds the given string key/value
func (e Entry) String(key string, value string) (entry Entry) {
if e.o.enc != nil {
e.o.String(key, value)
}
return e
}
// Null adds a null value for the given key
func (e Entry) Null(key string) (entry Entry) {
if e.o.enc != nil {
e.o.Null(key)
}
return e
}
// Error adds the given error key/value
func (e Entry) Error(key string, value error) (entry Entry) {
if e.o.enc != nil {
e.o.Error(key, value)
}
return e
}
// Time adds the given time key/value as an ISO8601 string
func (e Entry) Time(key string, value time.Time) (entry Entry) {
if e.o.enc != nil {
e.o.String(key, value.Format(time.RFC3339))
}
return e
}
// Duration adds the given duration key/value as a string
func (e Entry) Duration(key string, value time.Duration) (entry Entry) {
if e.o.enc != nil {
e.o.String(key, value.String())
}
return e
}
// Printf parses the format and args adding it as a key/string value in the log entry.
// This method is helpful to avoid allocations and extra work when logging with a lower
// log level than the logger is working with.
func (e Entry) Printf(key, format string, args ...interface{}) (entry Entry) {
if e.o.enc != nil {
e.o.String(key, fmt.Sprintf(format, args...))
}
return e
}
func (e Entry) init(level Level) {
t := time.Now()
e.level = level
if e.l.config.EnableTime {
e.o.enc.addKey(e.l.config.TimeField)
switch e.l.config.TimeFormat {
case Unix:
e.o.enc.AppendInt64(t.Unix())
case UnixMilli:
e.o.enc.AppendInt64(t.UnixNano() / int64(time.Millisecond))
case UnixNano:
e.o.enc.AppendInt64(t.UnixNano())
default:
e.o.enc.data = append(e.o.enc.data, '"')
e.o.enc.data = t.AppendFormat(e.o.enc.data, e.l.config.TimeFormat)
e.o.enc.data = append(e.o.enc.data, '"')
}
}
e.String(e.l.config.LevelField, level.String())
if e.l.config.EnableCaller {
_, f, l, ok := runtime.Caller(3 + e.l.config.CallerSkip)
if ok {
idx := strings.LastIndexByte(f, '/')
idx = strings.LastIndexByte(f[:idx], '/')
e.String("caller", f[idx+1:]+":"+strconv.Itoa(l))
} else {
e.String("caller", "???")
}
}
}