Merge pull request #2931 from MichaelEischer/reduce-debug-overhead

Reduce debug log overhead
This commit is contained in:
Alexander Neumann 2020-10-09 21:35:11 +02:00 committed by GitHub
commit 35655a481b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View file

@ -17,9 +17,10 @@ import (
) )
var opts struct { var opts struct {
logger *log.Logger isEnabled bool
funcs map[string]bool logger *log.Logger
files map[string]bool funcs map[string]bool
files map[string]bool
} }
// make sure that all the initialization happens before the init() functions // make sure that all the initialization happens before the init() functions
@ -30,6 +31,12 @@ func initDebug() bool {
initDebugLogger() initDebugLogger()
initDebugTags() initDebugTags()
if opts.logger == nil && len(opts.funcs) == 0 && len(opts.files) == 0 {
opts.isEnabled = false
return false
}
opts.isEnabled = true
fmt.Fprintf(os.Stderr, "debug enabled\n") fmt.Fprintf(os.Stderr, "debug enabled\n")
return true return true
@ -173,6 +180,10 @@ func checkFilter(filter map[string]bool, key string) bool {
// Log prints a message to the debug log (if debug is enabled). // Log prints a message to the debug log (if debug is enabled).
func Log(f string, args ...interface{}) { func Log(f string, args ...interface{}) {
if !opts.isEnabled {
return
}
fn, dir, file, line := getPosition() fn, dir, file, line := getPosition()
goroutine := goroutineNum() goroutine := goroutineNum()

View file

@ -66,7 +66,12 @@ type loggingRoundTripper struct {
// RoundTripper returns a new http.RoundTripper which logs all requests (if // RoundTripper returns a new http.RoundTripper which logs all requests (if
// debug is enabled). When debug is not enabled, upstream is returned. // debug is enabled). When debug is not enabled, upstream is returned.
func RoundTripper(upstream http.RoundTripper) http.RoundTripper { func RoundTripper(upstream http.RoundTripper) http.RoundTripper {
return loggingRoundTripper{eofDetectRoundTripper{upstream}} eofRoundTripper := eofDetectRoundTripper{upstream}
if opts.isEnabled {
// only use loggingRoundTripper if the debug log is configured
return loggingRoundTripper{eofRoundTripper}
}
return eofRoundTripper
} }
func (tr loggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) { func (tr loggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {