Skip to content

Commit

Permalink
Add frame count to WithIndirectCaller.
Browse files Browse the repository at this point in the history
  • Loading branch information
efritz committed Oct 7, 2019
1 parent e96cc14 commit 1f42b92
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Sometimes it is useful to define helper functions that logs messages for you in

```go
func logForMe(message string) {
parentLogger.WithIndirectCaller().Log(message)
parentLogger.WithIndirectCaller(1).Log(message)
}

logForMe("foobar")
Expand Down
2 changes: 1 addition & 1 deletion base_logger_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion caller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (s *CallerSuite) testAdapter(init func(*Config) (Logger, error)) {
logger, err := init(&Config{LogLevel: "info", LogEncoding: "json"})
Expect(err).To(BeNil())
// Push caller stack out once for each indirection
indirectLogger := logger.WithIndirectCaller().WithIndirectCaller().WithIndirectCaller()
indirectLogger := logger.WithIndirectCaller(3)

log3 := func(message string) { indirectLogger.Info(message) }
log2 := func(message string) { log3(message) }
Expand Down
2 changes: 1 addition & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package log

type (
Logger interface {
WithIndirectCaller() Logger
WithIndirectCaller(frames int) Logger
WithFields(LogFields) Logger
LogWithFields(LogLevel, LogFields, string, ...interface{})
Sync() error
Expand Down
29 changes: 16 additions & 13 deletions mocks/logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ func adaptReplayShim(shim *replayShim) ReplayLogger {
return &replayShimAdapter{adaptShim(shim), shim}
}

func (sa *shimAdapter) WithIndirectCaller() Logger {
return &shimAdapter{shim: sa.shim, depth: sa.depth + 1}
func (sa *shimAdapter) WithIndirectCaller(frames int) Logger {
if frames <= 0 {
panic("WithIndirectCaller called with invalid frame count")
}

return &shimAdapter{shim: sa.shim, depth: sa.depth + frames}
}

func (sa *shimAdapter) WithFields(fields LogFields) Logger {
Expand Down

0 comments on commit 1f42b92

Please sign in to comment.