Skip to content

Commit 2e59705

Browse files
authored
Merge pull request #4 from cabify/colega/no-debug-logger
Added NoDebugLogger that doesn't format debug logs
2 parents ec9d7cc + 7aabe92 commit 2e59705

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

no_debug_logger.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package log
2+
3+
// NoDebugLogger embeds a Logger, but in calls to debug functions it does nothing.
4+
// It avoids doing fmt.Sprintf() for those calls as they will be discarded anyways.
5+
// This makes those calls like 50 times faster (see benchmark file)
6+
type NoDebugLogger struct {
7+
Logger
8+
}
9+
10+
func (NoDebugLogger) Debug(args ...interface{}) {}
11+
func (NoDebugLogger) Debugf(format string, args ...interface{}) {}
12+
func (NoDebugLogger) Debugln(args ...interface{}) {}

no_debug_logger_bench_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package log
2+
3+
import (
4+
"testing"
5+
)
6+
7+
/*
8+
$ go test -bench=. ./...
9+
| goos: darwin
10+
| goarch: amd64
11+
| pkg: github.com/cabify/go-logging
12+
| BenchmarkPlainLoggerDebugSpeed-4 30000000 45.9 ns/op
13+
| BenchmarkNoDebugLoggerDebugSpeed-4 2000000000 0.33 ns/op
14+
| BenchmarkPlainLoggerDebugfSpeed-4 20000000 80.8 ns/op
15+
| BenchmarkNoDebugLoggerDebugfSpeed-4 50000000 36.5 ns/op
16+
*/
17+
18+
type someStruct struct {
19+
value float64
20+
}
21+
22+
var (
23+
structValue = someStruct{value: 10.0}
24+
plainLoggerInstance = NewLogger("test")
25+
noDebugLoggerInstance = NoDebugLogger{Logger: plainLoggerInstance}
26+
)
27+
28+
func BenchmarkPlainLoggerDebugSpeed(b *testing.B) {
29+
for i := 0; i < b.N; i++ {
30+
plainLoggerInstance.Debug("Something")
31+
}
32+
}
33+
34+
func BenchmarkNoDebugLoggerDebugSpeed(b *testing.B) {
35+
for i := 0; i < b.N; i++ {
36+
noDebugLoggerInstance.Debug("Something")
37+
}
38+
}
39+
40+
func BenchmarkPlainLoggerDebugfSpeed(b *testing.B) {
41+
for i := 0; i < b.N; i++ {
42+
plainLoggerInstance.Debugf("Something %v, %d", structValue, i)
43+
}
44+
}
45+
46+
func BenchmarkNoDebugLoggerDebugfSpeed(b *testing.B) {
47+
for i := 0; i < b.N; i++ {
48+
noDebugLoggerInstance.Debugf("Something %v, %d", structValue, i)
49+
}
50+
}

0 commit comments

Comments
 (0)