pass program counter to CallerMarshalFunc (#457)
This commit is contained in:
parent
4099072c03
commit
d894f123bc
4
event.go
4
event.go
|
@ -744,11 +744,11 @@ func (e *Event) caller(skip int) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
_, file, line, ok := runtime.Caller(skip + e.skipFrame)
|
pc, file, line, ok := runtime.Caller(skip + e.skipFrame)
|
||||||
if !ok {
|
if !ok {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
e.buf = enc.AppendString(enc.AppendKey(e.buf, CallerFieldName), CallerMarshalFunc(file, line))
|
e.buf = enc.AppendString(enc.AppendKey(e.buf, CallerFieldName), CallerMarshalFunc(pc, file, line))
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ var (
|
||||||
CallerSkipFrameCount = 2
|
CallerSkipFrameCount = 2
|
||||||
|
|
||||||
// CallerMarshalFunc allows customization of global caller marshaling
|
// CallerMarshalFunc allows customization of global caller marshaling
|
||||||
CallerMarshalFunc = func(file string, line int) string {
|
CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
||||||
return file + ":" + strconv.Itoa(line)
|
return file + ":" + strconv.Itoa(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
log_test.go
10
log_test.go
|
@ -782,7 +782,7 @@ func TestCallerMarshalFunc(t *testing.T) {
|
||||||
|
|
||||||
// test default behaviour this is really brittle due to the line numbers
|
// test default behaviour this is really brittle due to the line numbers
|
||||||
// actually mattering for validation
|
// actually mattering for validation
|
||||||
_, file, line, _ := runtime.Caller(0)
|
pc, file, line, _ := runtime.Caller(0)
|
||||||
caller := fmt.Sprintf("%s:%d", file, line+2)
|
caller := fmt.Sprintf("%s:%d", file, line+2)
|
||||||
log.Log().Caller().Msg("msg")
|
log.Log().Caller().Msg("msg")
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want {
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want {
|
||||||
|
@ -793,16 +793,16 @@ func TestCallerMarshalFunc(t *testing.T) {
|
||||||
// test custom behavior. In this case we'll take just the last directory
|
// test custom behavior. In this case we'll take just the last directory
|
||||||
origCallerMarshalFunc := CallerMarshalFunc
|
origCallerMarshalFunc := CallerMarshalFunc
|
||||||
defer func() { CallerMarshalFunc = origCallerMarshalFunc }()
|
defer func() { CallerMarshalFunc = origCallerMarshalFunc }()
|
||||||
CallerMarshalFunc = func(file string, line int) string {
|
CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
||||||
parts := strings.Split(file, "/")
|
parts := strings.Split(file, "/")
|
||||||
if len(parts) > 1 {
|
if len(parts) > 1 {
|
||||||
return strings.Join(parts[len(parts)-2:], "/") + ":" + strconv.Itoa(line)
|
return strings.Join(parts[len(parts)-2:], "/") + ":" + strconv.Itoa(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
return file + ":" + strconv.Itoa(line)
|
return runtime.FuncForPC(pc).Name() + ":" + file + ":" + strconv.Itoa(line)
|
||||||
}
|
}
|
||||||
_, file, line, _ = runtime.Caller(0)
|
pc, file, line, _ = runtime.Caller(0)
|
||||||
caller = CallerMarshalFunc(file, line+2)
|
caller = CallerMarshalFunc(pc, file, line+2)
|
||||||
log.Log().Caller().Msg("msg")
|
log.Log().Caller().Msg("msg")
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want {
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want {
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||||
|
|
Loading…
Reference in New Issue