diff --git a/field_test.go b/field_test.go index 91e4fc7ff..10ae0d1d7 100644 --- a/field_test.go +++ b/field_test.go @@ -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) } diff --git a/hook.go b/hook.go index 039e235b3..927a9e40d 100644 --- a/hook.go +++ b/hook.go @@ -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 { diff --git a/hook_test.go b/hook_test.go index ec4075682..a71a2fcf0 100644 --- a/hook_test.go +++ b/hook_test.go @@ -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))