Skip to content

Commit

Permalink
cleanup + refactor (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Karn authored May 31, 2022
1 parent 5b600a0 commit b53ceba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
20 changes: 10 additions & 10 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ type Entry struct {
start time.Time
}

func newEntry(e Entry) Entry {
fields := make([]Field, 0, len(e.Fields))
e.Fields = append(fields, e.Fields...)
func (e Entry) clone(fields ...Field) Entry {
f := make([]Field, 0, len(e.Fields)+len(fields))
e.Fields = append(f, e.Fields...)
e.Fields = append(f, fields...)
return e
}

func newEntryWithFields(fields []Field) Entry {
func newEntry(fields ...Field) Entry {
e := Entry{
Fields: make([]Field, 0, len(fields)),
Fields: make([]Field, 0, len(fields)+len(logFields)),
}
e.Fields = append(e.Fields, logFields...)
e.Fields = append(e.Fields, fields...)
return e
}

// WithField returns a new log entry with the supplied field.
func (e Entry) WithField(key string, value interface{}) Entry {
ne := newEntry(e)
ne.Fields = append(ne.Fields, Field{Key: key, Value: value})
ne := e.clone(Field{Key: key, Value: value})
return ne
}

// WithFields returns a new log entry with the supplied fields appended
func (e Entry) WithFields(fields ...Field) Entry {
ne := newEntry(e)
ne.Fields = append(ne.Fields, fields...)
ne := e.clone(fields...)
return ne
}

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

// WithError add a minimal stack trace to the log Entry
func (e Entry) WithError(err error) Entry {
return withErrFn(e, err)
return withErrFn(e.clone(), err)
}

// Debug logs a debug entry
Expand Down
13 changes: 6 additions & 7 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func errorsWithError(e Entry, err error) Entry {
ne := newEntry(e)
frame := runtimeext.StackLevel(2)

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

sourceBuff := BytePool().Get()
sourceBuff.B = extractSource(sourceBuff.B, frame)
ne.Fields = append(ne.Fields, Field{Key: "source", Value: string(sourceBuff.B[:len(sourceBuff.B)-1])})
e.Fields = append(e.Fields, Field{Key: "source", Value: string(sourceBuff.B[:len(sourceBuff.B)-1])})
BytePool().Put(sourceBuff)
ne.Fields = append(ne.Fields, Field{Key: "error", Value: string(errorBuff.B[:len(errorBuff.B)-1])})
e.Fields = append(e.Fields, Field{Key: "error", Value: string(errorBuff.B[:len(errorBuff.B)-1])})
BytePool().Put(errorBuff)

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

default:
errorBuff := BytePool().Get()
errorBuff.B = extractSource(errorBuff.B, frame)
errorBuff.B = append(errorBuff.B, err.Error()...)
ne.Fields = append(ne.Fields, Field{Key: "error", Value: string(errorBuff.B)})
e.Fields = append(e.Fields, Field{Key: "error", Value: string(errorBuff.B)})
BytePool().Put(errorBuff)
}
return ne
return e
}

func extractSource(b []byte, source runtimeext.Frame) []byte {
Expand Down
44 changes: 21 additions & 23 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func SetContext(ctx context.Context, e Entry) context.Context {
func GetContext(ctx context.Context) Entry {
v := ctx.Value(ctxIdent)
if v == nil {
return newEntryWithFields(nil)
return newEntry()
}
return v.(Entry)
}
Expand Down Expand Up @@ -178,125 +178,123 @@ func WithDefaultFields(fields ...Field) {

// WithField returns a new log entry with the supplied field.
func WithField(key string, value interface{}) Entry {
ne := newEntryWithFields(logFields)
ne.Fields = append(ne.Fields, Field{Key: key, Value: value})
ne := newEntry(Field{Key: key, Value: value})
return ne
}

// WithFields returns a new log entry with the supplied fields appended
func WithFields(fields ...Field) Entry {
ne := newEntryWithFields(logFields)
ne.Fields = append(ne.Fields, fields...)
ne := newEntry(fields...)
return ne
}

// WithTrace with add duration of how long the between this function call and
// the subsequent log
func WithTrace() Entry {
ne := newEntryWithFields(logFields)
ne := newEntry()
ne.start = time.Now()
return ne
}

// WithError add a minimal stack trace to the log Entry
func WithError(err error) Entry {
ne := newEntryWithFields(logFields)
ne := newEntry()
return withErrFn(ne, err)
}

// Debug logs a debug entry
func Debug(v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Debug(v...)
}

// Debugf logs a debug entry with formatting
func Debugf(s string, v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Debugf(s, v...)
}

// Info logs a normal. information, entry
func Info(v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Info(v...)
}

// Infof logs a normal. information, entry with formatting
func Infof(s string, v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Infof(s, v...)
}

// Notice logs a notice log entry
func Notice(v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Notice(v...)
}

// Noticef logs a notice log entry with formatting
func Noticef(s string, v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Noticef(s, v...)
}

// Warn logs a warning log entry
func Warn(v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Warn(v...)
}

// Warnf logs a warning log entry with formatting
func Warnf(s string, v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Warnf(s, v...)
}

// Panic logs a panic log entry
func Panic(v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Panic(v...)
}

// Panicf logs a panic log entry with formatting
func Panicf(s string, v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Panicf(s, v...)
}

// Alert logs an alert log entry
func Alert(v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Alert(v...)
}

// Alertf logs an alert log entry with formatting
func Alertf(s string, v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Alertf(s, v...)
}

// Fatal logs a fatal log entry
func Fatal(v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Fatal(v...)
}

// Fatalf logs a fatal log entry with formatting
func Fatalf(s string, v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Fatalf(s, v...)
}

// Error logs an error log entry
func Error(v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Error(v...)
}

// Errorf logs an error log entry with formatting
func Errorf(s string, v ...interface{}) {
e := newEntryWithFields(logFields)
e := newEntry()
e.Errorf(s, v...)
}

Expand Down

0 comments on commit b53ceba

Please sign in to comment.