merge upstream, add tests, full method context
- added benchmarks, and assertions for timeliness
- replaced regex usage in package-name handling with a straight
comparison to a cached value
- log the fullly-qualified method name, to remove ambiguity
eg: github.com/fflintstone/yabba.dabba.doo
- clean up possibly-unsafe assumptions about using the standard logger,
remove global lookup function to enforce safe usage
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -312,6 +313,53 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
|
||||
logger.ReportCaller = false // return to default value
|
||||
}
|
||||
|
||||
func logLoop(iterations int, reportCaller bool) {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
logger := New()
|
||||
logger.Out = &buffer
|
||||
logger.Formatter = new(JSONFormatter)
|
||||
logger.ReportCaller = reportCaller
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
logger.Infof("round %d of %d", i, iterations)
|
||||
}
|
||||
}
|
||||
|
||||
// Assertions for upper bounds to reporting overhead
|
||||
func TestCallerReportingOverhead(t *testing.T) {
|
||||
iterations := 10000
|
||||
before := time.Now()
|
||||
logLoop(iterations, false)
|
||||
during := time.Now()
|
||||
logLoop(iterations, true)
|
||||
after := time.Now()
|
||||
|
||||
elapsedNotReporting := during.Sub(before).Nanoseconds()
|
||||
elapsedReporting := after.Sub(during).Nanoseconds()
|
||||
|
||||
maxDelta := 1 * time.Second
|
||||
assert.WithinDuration(t, during, before, maxDelta,
|
||||
"%d log calls without caller name lookup takes less than %d second(s) (was %d nanoseconds)",
|
||||
iterations, maxDelta.Seconds(), elapsedNotReporting)
|
||||
assert.WithinDuration(t, after, during, maxDelta,
|
||||
"%d log calls without caller name lookup takes less than %d second(s) (was %d nanoseconds)",
|
||||
iterations, maxDelta.Seconds(), elapsedReporting)
|
||||
}
|
||||
|
||||
// benchmarks for both with and without caller-function reporting
|
||||
func BenchmarkWithoutCallerTracing(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
logLoop(1000, false)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkWithCallerTracing(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
logLoop(1000, true)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertLevelToString(t *testing.T) {
|
||||
assert.Equal(t, "debug", DebugLevel.String())
|
||||
assert.Equal(t, "info", InfoLevel.String())
|
||||
|
||||
Reference in New Issue
Block a user