Skip to content

Commit

Permalink
feat: add stack and caller (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored May 26, 2021
1 parent 6a218c2 commit 3045874
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
26 changes: 26 additions & 0 deletions ctx/ctx.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package ctx

import (
"fmt"
"runtime"
"strconv"
"time"

"github.com/go-stack/stack"
"github.com/hamba/logger/v2"
)

Expand Down Expand Up @@ -132,6 +136,28 @@ func Error(k string, err error) logger.Field {
}
}

// Stack return a stack string context field.
func Stack(k string) logger.Field {
return func(e *logger.Event) {
cs := stack.Trace().TrimRuntime()

c := stack.Caller(3)
cs = cs.TrimBelow(c)

e.AppendString(k, fmt.Sprintf("%+v", cs))
}
}

// Caller returns a caller string context field.
func Caller(k string) logger.Field {
return func(e *logger.Event) {
_, file, line, ok := runtime.Caller(3)
if ok {
e.AppendString(k, file+":"+strconv.Itoa(line))
}
}
}

// Time returns a time context field.
func Time(k string, t time.Time) logger.Field {
return func(e *logger.Event) {
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/hamba/logger/v2

go 1.16

require github.com/stretchr/testify v1.7.0
require (
github.com/go-stack/stack v1.8.0
github.com/stretchr/testify v1.7.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
18 changes: 17 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"errors"
"io"
"runtime"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -183,6 +185,9 @@ func TestLogger_Context(t *testing.T) {
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Info).With(ctx.Str("_n", "bench"), ctx.Int("_p", 1))

_, file, line, _ := runtime.Caller(0)
caller := file+":"+strconv.Itoa(line+3)

log.Info("some message",
ctx.Str("str", "string"),
ctx.Strs("strs", []string{"string1", "string2"}),
Expand All @@ -205,8 +210,19 @@ func TestLogger_Context(t *testing.T) {
ctx.Time("str", time.Unix(1541573670, 0).UTC()),
ctx.Duration("str", time.Second),
ctx.Interface("str", obj),
ctx.Caller("caller"),
)

want := `lvl=info msg="some message" _n=bench _p=1 str=string strs=string1,string2 bytes=98,121,116,101,115 bool=true int=1 ints=1,2,3 int8=2 int16=3 int32=4 int64=5 uint=1 uint8=2 uint16=3 uint32=4 uint64=5 float32=1.230 float64=4.560 err="test error" str=2018-11-07T06:54:30+0000 str=1s str={Name:test}` + "\n"
want := `lvl=info msg="some message" _n=bench _p=1 str=string strs=string1,string2 bytes=98,121,116,101,115 bool=true int=1 ints=1,2,3 int8=2 int16=3 int32=4 int64=5 uint=1 uint8=2 uint16=3 uint32=4 uint64=5 float32=1.230 float64=4.560 err="test error" str=2018-11-07T06:54:30+0000 str=1s str={Name:test} caller=`+caller + "\n"
assert.Equal(t, want, buf.String())
}

func TestLogger_Stack(t *testing.T) {
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Info)

log.Info("some message", ctx.Stack("stack"))

want := `lvl=info msg="some message" stack=[github.com/hamba/logger/logger/logger_test.go:224]` + "\n"
assert.Equal(t, want, buf.String())
}

0 comments on commit 3045874

Please sign in to comment.