Skip to content

Commit

Permalink
feat: support stack trace decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
damianopetrungaro committed Apr 27, 2022
1 parent f4984ba commit 6ab7781
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
33 changes: 28 additions & 5 deletions decorator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package golog

import (
"fmt"
"runtime"
"time"
)

Expand Down Expand Up @@ -46,21 +48,42 @@ func (td TimestampDecorator) Decorate(e Entry) Entry {
// StackTraceDecorator is a Decorator which add the log stacktrace
type StackTraceDecorator struct {
StacktraceFieldName string
Depth int
}

// NewStackTraceDecorator returns a StackTraceDecorator with the given field name
func NewStackTraceDecorator(n string) StackTraceDecorator {
return StackTraceDecorator{StacktraceFieldName: n}
func NewStackTraceDecorator(n string, depth int) StackTraceDecorator {
return StackTraceDecorator{StacktraceFieldName: n, Depth: depth}
}

// NewStackTraceDecoratorOption returns an Option which applies a StackTraceDecorator with the given field name
func NewStackTraceDecoratorOption(n string) Option {
func NewStackTraceDecoratorOption(n string, depth int) Option {
return OptionFunc(func(l StdLogger) StdLogger {
return l.WithDecorator(NewStackTraceDecorator(n))
return l.WithDecorator(NewStackTraceDecorator(n, depth))
})
}

// Decorate adds the stacktrace to the entry
func (sd StackTraceDecorator) Decorate(e Entry) Entry {
return e.With(Fields{String(sd.StacktraceFieldName, "TODO")})
framesToField := func(fs *runtime.Frames) Field {
var trace []string
for {
f, ok := fs.Next()
trace = append(trace, fmt.Sprintf("%v:%v", f.File, f.Line))
if !ok {
break
}
}

return Strings(sd.StacktraceFieldName, trace)
}

const skip = 4
pc := make([]uintptr, sd.Depth)
if n := runtime.Callers(skip, pc[:]); n != 0 {
field := framesToField(runtime.CallersFrames(pc[:]))
return e.With(Fields{field})
}

return e
}
49 changes: 49 additions & 0 deletions decorator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package golog_test

import (
"context"
"strings"
"testing"

. "github.com/damianopetrungaro/golog"
)

func TestStackTraceDecorator_Decorate(t *testing.T) {
const fieldName = "stacktrace"

w := &FakeWriter{}
logger := New(w).WithDecorator(NewStackTraceDecorator(fieldName, 2))

wantStack := []string{
"/github.com/damianopetrungaro/golog/decorator_test.go:24",
"/usr/local/go/src/testing/testing.go:1439",
}

ctx := context.Background()

logger.With(Fields{String("hello", "world")}).Info(ctx, "An info message")
for _, f := range w.Entry.Fields() {
if f.Key() != fieldName {
continue
}
stack, ok := f.Value().([]string)
if !ok {
break
}

for i, trace := range stack {
if strings.HasSuffix(trace, "/github.com/damianopetrungaro/golog/decorator_test.go:24") {
stack[i] = "/github.com/damianopetrungaro/golog/decorator_test.go:24"
}
}

if strings.Join(stack, "##") != strings.Join(wantStack, "##") {
t.Error("could not match trace")
t.Errorf("got: %v", stack)
t.Errorf("want: %v", wantStack)
}

return
}
t.Error("could not find stacktrace field")
}
7 changes: 5 additions & 2 deletions golog.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
),
levelChecker,
timestampDecorator,
stackTraceDecorator,
)

checkLogger CheckLogger = New(
Expand All @@ -29,14 +30,16 @@ var (
),
levelChecker,
timestampDecorator,
stackTraceDecorator,
)

errorHandler = func(err error) {
fmt.Println(fmt.Sprintf("golog: could not write: %s\n", err))
}

levelChecker = NewLevelCheckerOption(INFO)
timestampDecorator = NewTimestampDecoratorOption("timestamp", time.RFC3339Nano)
levelChecker = NewLevelCheckerOption(INFO)
timestampDecorator = NewTimestampDecoratorOption("timestamp", time.RFC3339Nano)
stackTraceDecorator = NewStackTraceDecoratorOption("stacktrace", 5)
)

// Message is a log entry message
Expand Down

0 comments on commit 6ab7781

Please sign in to comment.