summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-logger/entry.go
blob: 11e383086525c67d480a9273b883dc1dca34165e (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package logger

import (
	"context"
	"fmt"
	"time"

	"codeberg.org/gruf/go-bytes"
)

// Entry defines an entry in the log, it is NOT safe for concurrent use
type Entry struct {
	ctx context.Context
	lvl LEVEL
	buf *bytes.Buffer
	log *Logger
}

// Context returns the current set Entry context.Context
func (e *Entry) Context() context.Context {
	return e.ctx
}

// WithContext updates Entry context value to the supplied
func (e *Entry) WithContext(ctx context.Context) *Entry {
	e.ctx = ctx
	return e
}

// Level appends the supplied level to the log entry, and sets the entry level.
// Please note this CAN be called and append log levels multiple times
func (e *Entry) Level(lvl LEVEL) *Entry {
	e.log.Format.AppendLevel(e.buf, lvl)
	e.buf.WriteByte(' ')
	e.lvl = lvl
	return e
}

// Timestamp appends the current timestamp to the log entry. Please note this
// CAN be called and append the timestamp multiple times
func (e *Entry) Timestamp() *Entry {
	e.log.Format.AppendTimestamp(e.buf, clock.NowFormat())
	e.buf.WriteByte(' ')
	return e
}

// TimestampIf performs Entry.Timestamp() only IF timestamping is enabled for the Logger.
// Please note this CAN be called multiple times
func (e *Entry) TimestampIf() *Entry {
	if e.log.Timestamp {
		e.Timestamp()
	}
	return e
}

// Hooks applies currently set Hooks to the Entry. Please note this CAN be
// called and perform the Hooks multiple times
func (e *Entry) Hooks() *Entry {
	for _, hook := range e.log.Hooks {
		hook.Do(e)
	}
	return e
}

// Byte appends a byte value to the log entry
func (e *Entry) Byte(value byte) *Entry {
	e.log.Format.AppendByte(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// ByteField appends a byte value as key-value pair to the log entry
func (e *Entry) ByteField(key string, value byte) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendByte(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Bytes appends a byte slice value as to the log entry
func (e *Entry) Bytes(value []byte) *Entry {
	e.log.Format.AppendBytes(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// BytesField appends a byte slice value as key-value pair to the log entry
func (e *Entry) BytesField(key string, value []byte) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendBytes(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Str appends a string value to the log entry
func (e *Entry) Str(value string) *Entry {
	e.log.Format.AppendString(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// StrField appends a string value as key-value pair to the log entry
func (e *Entry) StrField(key string, value string) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendString(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Strs appends a string slice value to the log entry
func (e *Entry) Strs(value []string) *Entry {
	e.log.Format.AppendStrings(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// StrsField appends a string slice value as key-value pair to the log entry
func (e *Entry) StrsField(key string, value []string) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendStrings(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Int appends an int value to the log entry
func (e *Entry) Int(value int) *Entry {
	e.log.Format.AppendInt(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// IntField appends an int value as key-value pair to the log entry
func (e *Entry) IntField(key string, value int) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendInt(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Ints appends an int slice value to the log entry
func (e *Entry) Ints(value []int) *Entry {
	e.log.Format.AppendInts(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// IntsField appends an int slice value as key-value pair to the log entry
func (e *Entry) IntsField(key string, value []int) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendInts(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Uint appends a uint value to the log entry
func (e *Entry) Uint(value uint) *Entry {
	e.log.Format.AppendUint(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// UintField appends a uint value as key-value pair to the log entry
func (e *Entry) UintField(key string, value uint) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendUint(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Uints appends a uint slice value to the log entry
func (e *Entry) Uints(value []uint) *Entry {
	e.log.Format.AppendUints(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// UintsField appends a uint slice value as key-value pair to the log entry
func (e *Entry) UintsField(key string, value []uint) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendUints(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Float appends a float value to the log entry
func (e *Entry) Float(value float64) *Entry {
	e.log.Format.AppendFloat(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// FloatField appends a float value as key-value pair to the log entry
func (e *Entry) FloatField(key string, value float64) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendFloat(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Floats appends a float slice value to the log entry
func (e *Entry) Floats(value []float64) *Entry {
	e.log.Format.AppendFloats(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// FloatsField appends a float slice value as key-value pair to the log entry
func (e *Entry) FloatsField(key string, value []float64) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendFloats(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Bool appends a bool value to the log entry
func (e *Entry) Bool(value bool) *Entry {
	e.log.Format.AppendBool(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// BoolField appends a bool value as key-value pair to the log entry
func (e *Entry) BoolField(key string, value bool) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendBool(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Bools appends a bool slice value to the log entry
func (e *Entry) Bools(value []bool) *Entry {
	e.log.Format.AppendBools(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// BoolsField appends a bool slice value as key-value pair to the log entry
func (e *Entry) BoolsField(key string, value []bool) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendBools(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Time appends a time.Time value to the log entry
func (e *Entry) Time(value time.Time) *Entry {
	e.log.Format.AppendTime(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// TimeField appends a time.Time value as key-value pair to the log entry
func (e *Entry) TimeField(key string, value time.Time) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendTime(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Times appends a time.Time slice value to the log entry
func (e *Entry) Times(value []time.Time) *Entry {
	e.log.Format.AppendTimes(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// TimesField appends a time.Time slice value as key-value pair to the log entry
func (e *Entry) TimesField(key string, value []time.Time) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendTimes(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// DurationField appends a time.Duration value to the log entry
func (e *Entry) Duration(value time.Duration) *Entry {
	e.log.Format.AppendDuration(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// DurationField appends a time.Duration value as key-value pair to the log entry
func (e *Entry) DurationField(key string, value time.Duration) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendDuration(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Durations appends a time.Duration slice value to the log entry
func (e *Entry) Durations(value []time.Duration) *Entry {
	e.log.Format.AppendDurations(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// DurationsField appends a time.Duration slice value as key-value pair to the log entry
func (e *Entry) DurationsField(key string, value []time.Duration) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendDurations(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Field appends an interface value as key-value pair to the log entry
func (e *Entry) Field(key string, value interface{}) *Entry {
	e.log.Format.AppendKey(e.buf, key)
	e.log.Format.AppendValue(e.buf, value)
	e.buf.WriteByte(' ')
	return e
}

// Fields appends a map of key-value pairs to the log entry
func (e *Entry) Fields(fields map[string]interface{}) *Entry {
	for key, value := range fields {
		e.Field(key, value)
	}
	return e
}

// Values appends the given values to the log entry formatted as values, without a key.
func (e *Entry) Values(values ...interface{}) *Entry {
	for _, value := range values {
		e.log.Format.AppendValue(e.buf, value)
		e.buf.WriteByte(' ')
	}
	return e
}

// Append will append the given args formatted using fmt.Sprint(a...) to the Entry.
func (e *Entry) Append(a ...interface{}) *Entry {
	fmt.Fprint(e.buf, a...)
	e.buf.WriteByte(' ')
	return e
}

// Appendf will append the given format string and args using fmt.Sprintf(s, a...) to the Entry.
func (e *Entry) Appendf(s string, a ...interface{}) *Entry {
	fmt.Fprintf(e.buf, s, a...)
	e.buf.WriteByte(' ')
	return e
}

// Msg appends the fmt.Sprint() formatted final message to the log and calls .Send()
func (e *Entry) Msg(a ...interface{}) {
	e.log.Format.AppendMsg(e.buf, a...)
	e.Send()
}

// Msgf appends the fmt.Sprintf() formatted final message to the log and calls .Send()
func (e *Entry) Msgf(s string, a ...interface{}) {
	e.log.Format.AppendMsgf(e.buf, s, a...)
	e.Send()
}

// Send triggers write of the log entry, skipping if the entry's log LEVEL
// is below the currently set Logger level, and releases the Entry back to
// the Logger's Entry pool. So it is NOT safe to continue using this Entry
// object after calling .Send(), .Msg() or .Msgf()
func (e *Entry) Send() {
	// If nothing to do, return
	if e.lvl < e.log.Level || e.buf.Len() < 1 {
		e.reset()
		return
	}

	// Ensure a final new line
	if e.buf.B[e.buf.Len()-1] != '\n' {
		e.buf.WriteByte('\n')
	}

	// Write, reset and release
	e.log.Output.Write(e.buf.B)
	e.reset()
}

func (e *Entry) reset() {
	// Reset all
	e.ctx = nil
	e.buf.Reset()
	e.lvl = unset

	// Release to pool
	e.log.pool.Put(e)
}