Allow custom caller level (#196)
To modify the Caller, you can pass a jump to a few function call stacks.
```go
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func myError() {
log.Debug().Caller(1).Str("test2", "v2").Send()
}
func foo() {
log.Debug().Caller(2).Str("test2", "v2").Send()
}
func myError2() {
foo()
}
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Debug().Caller().Str("test1", "v1").Send()
myError()
myError2()
}
```
This commit is contained in:
parent
4502cc1942
commit
e709c5d91e
10
event.go
10
event.go
@ -665,8 +665,14 @@ func (e *Event) Interface(key string, i interface{}) *Event {
|
||||
}
|
||||
|
||||
// Caller adds the file:line of the caller with the zerolog.CallerFieldName key.
|
||||
func (e *Event) Caller() *Event {
|
||||
return e.caller(CallerSkipFrameCount)
|
||||
// The argument skip is the number of stack frames to ascend
|
||||
// Skip If not passed, use the global variable CallerSkipFrameCount
|
||||
func (e *Event) Caller(skip ...int) *Event {
|
||||
sk := CallerSkipFrameCount
|
||||
if len(skip) > 0 {
|
||||
sk = skip[0] + CallerSkipFrameCount
|
||||
}
|
||||
return e.caller(sk)
|
||||
}
|
||||
|
||||
func (e *Event) caller(skip int) *Event {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user