forked from mirror/logrus
test updates
- add tests for callers accessed directly or via function pointer (results are unchanged) - undo unwanted capitalization/export in previous commit
This commit is contained in:
parent
7d48cb786e
commit
3cb9e18ef9
8
entry.go
8
entry.go
|
@ -13,7 +13,7 @@ import (
|
||||||
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
|
var 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
|
var minimumCallerDepth int
|
||||||
|
@ -126,8 +126,8 @@ 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 == "" {
|
if LogrusPackage == "" {
|
||||||
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
|
||||||
minimumCallerDepth = knownLogrusFrames
|
minimumCallerDepth = knownLogrusFrames
|
||||||
|
@ -138,7 +138,7 @@ func getCaller() (method string) {
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ package logrus
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// DefaultTimestampFormat is YYYY-mm-DDTHH:MM:SS-TZ
|
// defaultTimestampFormat is YYYY-mm-DDTHH:MM:SS-TZ
|
||||||
const DefaultTimestampFormat = time.RFC3339
|
const defaultTimestampFormat = time.RFC3339
|
||||||
|
|
||||||
// The Formatter interface is used to implement a custom Formatter. It takes an
|
// The Formatter interface is used to implement a custom Formatter. It takes an
|
||||||
// `Entry`. It exposes all the fields, including the default ones:
|
// `Entry`. It exposes all the fields, including the default ones:
|
||||||
|
|
|
@ -59,7 +59,9 @@ func LogAndAssertText(t *testing.T, log func(*Logger), assertions func(fields ma
|
||||||
|
|
||||||
// TestReportCaller verifies that when ReportCaller is set, the 'func' field
|
// TestReportCaller verifies that when ReportCaller is set, the 'func' field
|
||||||
// is added, and when it is unset it is not set or modified
|
// is added, and when it is unset it is not set or modified
|
||||||
func TestReportCaller(t *testing.T) {
|
// Verify that functions within the Logrus package aren't considered when
|
||||||
|
// discovering the caller.
|
||||||
|
func TestReportCallerWhenConfigured(t *testing.T) {
|
||||||
LogAndAssertJSON(t, func(log *Logger) {
|
LogAndAssertJSON(t, func(log *Logger) {
|
||||||
log.ReportCaller = false
|
log.ReportCaller = false
|
||||||
log.Print("testNoCaller")
|
log.Print("testNoCaller")
|
||||||
|
@ -79,6 +81,51 @@ func TestReportCaller(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logSomething(t *testing.T, message string) Fields {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
var fields Fields
|
||||||
|
|
||||||
|
logger := New()
|
||||||
|
logger.Out = &buffer
|
||||||
|
logger.Formatter = new(JSONFormatter)
|
||||||
|
logger.ReportCaller = true
|
||||||
|
|
||||||
|
// override the filter to allow reporting of functions within the logrus package
|
||||||
|
LogrusPackage = "bogusForTesting"
|
||||||
|
|
||||||
|
entry := logger.WithFields(Fields{
|
||||||
|
"foo": "bar",
|
||||||
|
})
|
||||||
|
|
||||||
|
entry.Info(message)
|
||||||
|
|
||||||
|
err := json.Unmarshal(buffer.Bytes(), &fields)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// now clear the override so as not to mess with other usage
|
||||||
|
LogrusPackage = ""
|
||||||
|
return fields
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestReportCallerHelperDirect - verify reference when logging from a regular function
|
||||||
|
func TestReportCallerHelperDirect(t *testing.T) {
|
||||||
|
fields := logSomething(t, "direct")
|
||||||
|
|
||||||
|
assert.Equal(t, "direct", fields["msg"])
|
||||||
|
assert.Equal(t, "info", fields["level"])
|
||||||
|
assert.Equal(t, "github.com/dclendenan/logrus.logSomething", fields["func"])
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestReportCallerHelperDirect - verify reference when logging from a function called via pointer
|
||||||
|
func TestReportCallerHelperViaPointer(t *testing.T) {
|
||||||
|
fptr := logSomething
|
||||||
|
fields := fptr(t, "via pointer")
|
||||||
|
|
||||||
|
assert.Equal(t, "via pointer", fields["msg"])
|
||||||
|
assert.Equal(t, "info", fields["level"])
|
||||||
|
assert.Equal(t, "github.com/dclendenan/logrus.logSomething", fields["func"])
|
||||||
|
}
|
||||||
|
|
||||||
func TestPrint(t *testing.T) {
|
func TestPrint(t *testing.T) {
|
||||||
LogAndAssertJSON(t, func(log *Logger) {
|
LogAndAssertJSON(t, func(log *Logger) {
|
||||||
log.Print("test")
|
log.Print("test")
|
||||||
|
|
Loading…
Reference in New Issue