forked from mirror/logrus
Use a sync.Once to init the reportCaller data
This commit is contained in:
parent
5fcd19eae6
commit
975c406ddb
26
entry.go
26
entry.go
|
@ -11,16 +11,23 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var bufferPool *sync.Pool
|
var (
|
||||||
|
bufferPool *sync.Pool
|
||||||
|
|
||||||
// qualified package name, cached at first use
|
// qualified package name, cached at first use
|
||||||
var LogrusPackage string
|
logrusPackage string
|
||||||
|
|
||||||
// Positions in the call stack when tracing to report the calling method
|
// Positions in the call stack when tracing to report the calling method
|
||||||
var minimumCallerDepth int
|
minimumCallerDepth int
|
||||||
|
|
||||||
const maximumCallerDepth int = 25
|
// Used for caller information initialisation
|
||||||
const knownLogrusFrames int = 4
|
callerInitOnce sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maximumCallerDepth int = 25
|
||||||
|
knownLogrusFrames int = 4
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
bufferPool = &sync.Pool{
|
bufferPool = &sync.Pool{
|
||||||
|
@ -143,19 +150,20 @@ func getCaller() (method string) {
|
||||||
depth := runtime.Callers(minimumCallerDepth, pcs)
|
depth := runtime.Callers(minimumCallerDepth, pcs)
|
||||||
|
|
||||||
// cache this package's fully-qualified name
|
// cache this package's fully-qualified name
|
||||||
if LogrusPackage == "" {
|
callerInitOnce.Do(func() {
|
||||||
LogrusPackage = getPackageName(runtime.FuncForPC(pcs[0]).Name())
|
logrusPackage = getPackageName(runtime.FuncForPC(pcs[0]).Name())
|
||||||
|
|
||||||
// now that we have the cache, we can skip a minimum count of known-logrus functions
|
// now that we have the cache, we can skip a minimum count of known-logrus functions
|
||||||
|
// XXX this is dubious, the number of frames may vary store an entry in a logger interface
|
||||||
minimumCallerDepth = knownLogrusFrames
|
minimumCallerDepth = knownLogrusFrames
|
||||||
}
|
})
|
||||||
|
|
||||||
for i := 0; i < depth; i++ {
|
for i := 0; i < depth; i++ {
|
||||||
fullFuncName := runtime.FuncForPC(pcs[i]).Name()
|
fullFuncName := runtime.FuncForPC(pcs[i]).Name()
|
||||||
pkg := getPackageName(fullFuncName)
|
pkg := getPackageName(fullFuncName)
|
||||||
|
|
||||||
// If the caller isn't part of this package, we're done
|
// If the caller isn't part of this package, we're done
|
||||||
if pkg != LogrusPackage {
|
if pkg != logrusPackage {
|
||||||
return fullFuncName
|
return fullFuncName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,7 +92,7 @@ func logSomething(t *testing.T, message string) Fields {
|
||||||
logger.ReportCaller = true
|
logger.ReportCaller = true
|
||||||
|
|
||||||
// override the filter to allow reporting of functions within the logrus package
|
// override the filter to allow reporting of functions within the logrus package
|
||||||
LogrusPackage = "bogusForTesting"
|
logrusPackage = "bogusForTesting"
|
||||||
|
|
||||||
entry := logger.WithFields(Fields{
|
entry := logger.WithFields(Fields{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
|
@ -104,7 +104,7 @@ func logSomething(t *testing.T, message string) Fields {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// now clear the override so as not to mess with other usage
|
// now clear the override so as not to mess with other usage
|
||||||
LogrusPackage = ""
|
logrusPackage = ""
|
||||||
return fields
|
return fields
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue