Skip to content

Commit

Permalink
Add AddCallers() and additional test cases around callers functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
phungs committed Dec 2, 2016
1 parent daeb166 commit 978b00d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
16 changes: 14 additions & 2 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,22 @@ func TestStackField(t *testing.T) {
func TestCaller(t *testing.T) {
// Caller() makes an assupmtion that it is called within zap's hook mechanism.
// This is not true for this test, so pass -1 to log this function as the caller.
c, _ := Caller(-1)
c, ok := Caller(-1)
assert.NoError(t, ok, "Caller should not return an error ")
assertFieldJSONRegEx(t, "\"caller\":{\"pc\":[0-9]+,\"file\":\".+field_test.go\",\"line\":[0-9]+}", c)

c2, _ := Caller(-1)
c2, ok := Caller(-1)
assert.NoError(t, ok, "Caller should not return an error ")
assertCanBeReused(t, c2)
}

func TestCallers(t *testing.T) {
// Caller() makes an assupmtion that it is called within zap's hook mechanism.
// This is not true for this test, so pass 0 to log this function as the caller.
c := Callers(0)
assertFieldJSONRegEx(t, "\"callers\":{\"0\":{\"pc\":[0-9]+,\"file\":\".+field_test.go\",\"line\":[0-9]+},", c)

c2 := Callers(0)
assertCanBeReused(t, c2)
}

Expand Down
10 changes: 8 additions & 2 deletions hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ func AddCallerWithSkip(callerSkip int) Option {
})
}

// AddCallersWithSkip records the caller's call stack for all messages at or above a given level.
// The callerskip option helps select the target caller. Using a value of _callerSkip will select
// AddCallers records the caller's call stack for all messages at or above a given level.
// This uses _callerSkip for the caller skip, which will select
// the target caller as the caller of the leveled logger method.
func AddCallers(lvl Level) Option {
return AddCallersWithSkip(_callersSkip, lvl)
}

// AddCallersWithSkip records the caller's call stack for all messages at or above a given level.
// The callerskip option helps select the target caller.
func AddCallersWithSkip(callerSkip int, lvl Level) Option {
return Hook(func(e *Entry) error {
if e == nil {
Expand Down
18 changes: 18 additions & 0 deletions hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ func TestHookAddCallerFail(t *testing.T) {
assert.Contains(t, buf.String(), `"msg":"Failure."`, "Expected original message to survive failures in runtime.Caller.")
}

func TestHookAddCallers(t *testing.T) {
buf := &testBuffer{}
logger := New(NewJSONEncoder(), DebugLevel, Output(buf), AddCallers(InfoLevel))

logger.Info("Callers.")
output := buf.String()
require.Contains(t, output, "zap/hook_test.go", "Expected to find test file in callers.")
assert.Contains(t, output, `"callers":`, "Callers added under an unexpected key.")

buf.Reset()
logger.Warn("Callers.")
assert.Contains(t, buf.String(), `"callers":`, "Expected to include callers at Warn level.")

buf.Reset()
logger.Debug("No callers.")
assert.NotContains(t, buf.String(), "Unexpected stacktrace at Debug level.")
}

func TestHookAddCallersWithSkip(t *testing.T) {
buf := &testBuffer{}
logger := New(NewJSONEncoder(), DebugLevel, Output(buf), AddCallersWithSkip(_callersSkip, InfoLevel))
Expand Down

0 comments on commit 978b00d

Please sign in to comment.