summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-logger/entry.go
blob: f2fe293aa5587b38da5f0a316063c311732b1bec (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
package logger

import (
	"context"
	"time"

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

// Entry defines an entry in the log
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 as key-value pair to the log entry
func (e *Entry) Byte(key string, value byte) *Entry {
	e.log.Format.AppendByteField(e.buf, key, value)
	e.buf.WriteByte(' ')
	return e
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// Durations appends a time.Duration slice value as key-value pair to the log entry
func (e *Entry) Durations(key string, value []time.Duration) *Entry {
	e.log.Format.AppendDurationsField(e.buf, key, 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.AppendField(e.buf, key, 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 {
	e.log.Format.AppendFields(e.buf, fields)
	e.buf.WriteByte(' ')
	return e
}

// Msg appends the 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 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
	}

	// 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)
}