Skip to content

Commit b53ceba

Browse files
author
Dean Karn
authored
cleanup + refactor (#42)
1 parent 5b600a0 commit b53ceba

File tree

3 files changed

+37
-40
lines changed

3 files changed

+37
-40
lines changed

entry.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@ type Entry struct {
1717
start time.Time
1818
}
1919

20-
func newEntry(e Entry) Entry {
21-
fields := make([]Field, 0, len(e.Fields))
22-
e.Fields = append(fields, e.Fields...)
20+
func (e Entry) clone(fields ...Field) Entry {
21+
f := make([]Field, 0, len(e.Fields)+len(fields))
22+
e.Fields = append(f, e.Fields...)
23+
e.Fields = append(f, fields...)
2324
return e
2425
}
2526

26-
func newEntryWithFields(fields []Field) Entry {
27+
func newEntry(fields ...Field) Entry {
2728
e := Entry{
28-
Fields: make([]Field, 0, len(fields)),
29+
Fields: make([]Field, 0, len(fields)+len(logFields)),
2930
}
31+
e.Fields = append(e.Fields, logFields...)
3032
e.Fields = append(e.Fields, fields...)
3133
return e
3234
}
3335

3436
// WithField returns a new log entry with the supplied field.
3537
func (e Entry) WithField(key string, value interface{}) Entry {
36-
ne := newEntry(e)
37-
ne.Fields = append(ne.Fields, Field{Key: key, Value: value})
38+
ne := e.clone(Field{Key: key, Value: value})
3839
return ne
3940
}
4041

4142
// WithFields returns a new log entry with the supplied fields appended
4243
func (e Entry) WithFields(fields ...Field) Entry {
43-
ne := newEntry(e)
44-
ne.Fields = append(ne.Fields, fields...)
44+
ne := e.clone(fields...)
4545
return ne
4646
}
4747

@@ -54,7 +54,7 @@ func (e Entry) WithTrace() Entry {
5454

5555
// WithError add a minimal stack trace to the log Entry
5656
func (e Entry) WithError(err error) Entry {
57-
return withErrFn(e, err)
57+
return withErrFn(e.clone(), err)
5858
}
5959

6060
// Debug logs a debug entry

errors.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
)
1010

1111
func errorsWithError(e Entry, err error) Entry {
12-
ne := newEntry(e)
1312
frame := runtimeext.StackLevel(2)
1413

1514
switch t := err.(type) {
@@ -43,24 +42,24 @@ func errorsWithError(e Entry, err error) Entry {
4342

4443
sourceBuff := BytePool().Get()
4544
sourceBuff.B = extractSource(sourceBuff.B, frame)
46-
ne.Fields = append(ne.Fields, Field{Key: "source", Value: string(sourceBuff.B[:len(sourceBuff.B)-1])})
45+
e.Fields = append(e.Fields, Field{Key: "source", Value: string(sourceBuff.B[:len(sourceBuff.B)-1])})
4746
BytePool().Put(sourceBuff)
48-
ne.Fields = append(ne.Fields, Field{Key: "error", Value: string(errorBuff.B[:len(errorBuff.B)-1])})
47+
e.Fields = append(e.Fields, Field{Key: "error", Value: string(errorBuff.B[:len(errorBuff.B)-1])})
4948
BytePool().Put(errorBuff)
5049

51-
ne.Fields = append(ne.Fields, tags...) // we do it this way to maintain order of error, source as first fields
50+
e.Fields = append(e.Fields, tags...) // we do it this way to maintain order of error, source as first fields
5251
if len(types) > 0 {
53-
ne.Fields = append(ne.Fields, Field{Key: "types", Value: string(types[:len(types)-1])})
52+
e.Fields = append(e.Fields, Field{Key: "types", Value: string(types[:len(types)-1])})
5453
}
5554

5655
default:
5756
errorBuff := BytePool().Get()
5857
errorBuff.B = extractSource(errorBuff.B, frame)
5958
errorBuff.B = append(errorBuff.B, err.Error()...)
60-
ne.Fields = append(ne.Fields, Field{Key: "error", Value: string(errorBuff.B)})
59+
e.Fields = append(e.Fields, Field{Key: "error", Value: string(errorBuff.B)})
6160
BytePool().Put(errorBuff)
6261
}
63-
return ne
62+
return e
6463
}
6564

6665
func extractSource(b []byte, source runtimeext.Frame) []byte {

log.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func SetContext(ctx context.Context, e Entry) context.Context {
7676
func GetContext(ctx context.Context) Entry {
7777
v := ctx.Value(ctxIdent)
7878
if v == nil {
79-
return newEntryWithFields(nil)
79+
return newEntry()
8080
}
8181
return v.(Entry)
8282
}
@@ -178,125 +178,123 @@ func WithDefaultFields(fields ...Field) {
178178

179179
// WithField returns a new log entry with the supplied field.
180180
func WithField(key string, value interface{}) Entry {
181-
ne := newEntryWithFields(logFields)
182-
ne.Fields = append(ne.Fields, Field{Key: key, Value: value})
181+
ne := newEntry(Field{Key: key, Value: value})
183182
return ne
184183
}
185184

186185
// WithFields returns a new log entry with the supplied fields appended
187186
func WithFields(fields ...Field) Entry {
188-
ne := newEntryWithFields(logFields)
189-
ne.Fields = append(ne.Fields, fields...)
187+
ne := newEntry(fields...)
190188
return ne
191189
}
192190

193191
// WithTrace with add duration of how long the between this function call and
194192
// the subsequent log
195193
func WithTrace() Entry {
196-
ne := newEntryWithFields(logFields)
194+
ne := newEntry()
197195
ne.start = time.Now()
198196
return ne
199197
}
200198

201199
// WithError add a minimal stack trace to the log Entry
202200
func WithError(err error) Entry {
203-
ne := newEntryWithFields(logFields)
201+
ne := newEntry()
204202
return withErrFn(ne, err)
205203
}
206204

207205
// Debug logs a debug entry
208206
func Debug(v ...interface{}) {
209-
e := newEntryWithFields(logFields)
207+
e := newEntry()
210208
e.Debug(v...)
211209
}
212210

213211
// Debugf logs a debug entry with formatting
214212
func Debugf(s string, v ...interface{}) {
215-
e := newEntryWithFields(logFields)
213+
e := newEntry()
216214
e.Debugf(s, v...)
217215
}
218216

219217
// Info logs a normal. information, entry
220218
func Info(v ...interface{}) {
221-
e := newEntryWithFields(logFields)
219+
e := newEntry()
222220
e.Info(v...)
223221
}
224222

225223
// Infof logs a normal. information, entry with formatting
226224
func Infof(s string, v ...interface{}) {
227-
e := newEntryWithFields(logFields)
225+
e := newEntry()
228226
e.Infof(s, v...)
229227
}
230228

231229
// Notice logs a notice log entry
232230
func Notice(v ...interface{}) {
233-
e := newEntryWithFields(logFields)
231+
e := newEntry()
234232
e.Notice(v...)
235233
}
236234

237235
// Noticef logs a notice log entry with formatting
238236
func Noticef(s string, v ...interface{}) {
239-
e := newEntryWithFields(logFields)
237+
e := newEntry()
240238
e.Noticef(s, v...)
241239
}
242240

243241
// Warn logs a warning log entry
244242
func Warn(v ...interface{}) {
245-
e := newEntryWithFields(logFields)
243+
e := newEntry()
246244
e.Warn(v...)
247245
}
248246

249247
// Warnf logs a warning log entry with formatting
250248
func Warnf(s string, v ...interface{}) {
251-
e := newEntryWithFields(logFields)
249+
e := newEntry()
252250
e.Warnf(s, v...)
253251
}
254252

255253
// Panic logs a panic log entry
256254
func Panic(v ...interface{}) {
257-
e := newEntryWithFields(logFields)
255+
e := newEntry()
258256
e.Panic(v...)
259257
}
260258

261259
// Panicf logs a panic log entry with formatting
262260
func Panicf(s string, v ...interface{}) {
263-
e := newEntryWithFields(logFields)
261+
e := newEntry()
264262
e.Panicf(s, v...)
265263
}
266264

267265
// Alert logs an alert log entry
268266
func Alert(v ...interface{}) {
269-
e := newEntryWithFields(logFields)
267+
e := newEntry()
270268
e.Alert(v...)
271269
}
272270

273271
// Alertf logs an alert log entry with formatting
274272
func Alertf(s string, v ...interface{}) {
275-
e := newEntryWithFields(logFields)
273+
e := newEntry()
276274
e.Alertf(s, v...)
277275
}
278276

279277
// Fatal logs a fatal log entry
280278
func Fatal(v ...interface{}) {
281-
e := newEntryWithFields(logFields)
279+
e := newEntry()
282280
e.Fatal(v...)
283281
}
284282

285283
// Fatalf logs a fatal log entry with formatting
286284
func Fatalf(s string, v ...interface{}) {
287-
e := newEntryWithFields(logFields)
285+
e := newEntry()
288286
e.Fatalf(s, v...)
289287
}
290288

291289
// Error logs an error log entry
292290
func Error(v ...interface{}) {
293-
e := newEntryWithFields(logFields)
291+
e := newEntry()
294292
e.Error(v...)
295293
}
296294

297295
// Errorf logs an error log entry with formatting
298296
func Errorf(s string, v ...interface{}) {
299-
e := newEntryWithFields(logFields)
297+
e := newEntry()
300298
e.Errorf(s, v...)
301299
}
302300

0 commit comments

Comments
 (0)