From 9b5e5a43d8e5cc4aac2d053cc808d072acef237a Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Wed, 7 Oct 2020 13:15:47 +0200 Subject: [PATCH] util/{log,netutil}: fix the log bridge Prior to this patch, log messages from the http package would look like this: ``` E201007 11:15:33.200128 878 httpLoggerserver.go:3088 http: ... ``` Notice how "httpLoggerserver.go" does not correspond to any valid file. With this patch: ``` E201007 11:15:33.200128 878 (gostd) net/http/server.go:3088 http: ... ``` Release note: None --- pkg/util/log/log_bridge.go | 12 ++++++++++-- pkg/util/netutil/net.go | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/util/log/log_bridge.go b/pkg/util/log/log_bridge.go index 09ae9c253cc1..fe6f299eaf54 100644 --- a/pkg/util/log/log_bridge.go +++ b/pkg/util/log/log_bridge.go @@ -16,13 +16,19 @@ import ( "fmt" stdLog "log" "strconv" + "strings" ) // NewStdLogger creates a *stdLog.Logger that forwards messages to the // CockroachDB logs with the specified severity. // -// The prefix appears at the beginning of each generated log line. +// The prefix should be the path of the package for which this logger +// is used. The prefix will be concatenated directly with the name +// of the file that triggered the logging. func NewStdLogger(severity Severity, prefix string) *stdLog.Logger { + if prefix != "" && !strings.HasSuffix(prefix, "/") { + prefix += "/" + } return stdLog.New(logBridge(severity), prefix, stdLog.Lshortfile) } @@ -67,7 +73,9 @@ func (lb logBridge) Write(b []byte) (n int, err error) { if parts := bytes.SplitN(b, []byte{':'}, 3); len(parts) != 3 || len(parts[0]) < 1 || len(parts[2]) < 1 { entry.Message = fmt.Sprintf("bad log format: %s", b) } else { - entry.File = string(parts[0]) + // We use a "(gostd)" prefix so that these log lines correctly point + // to the go standard library instead of our own source directory. + entry.File = "(gostd) " + string(parts[0]) entry.Message = string(parts[2][1 : len(parts[2])-1]) // skip leading space and trailing newline entry.Line, err = strconv.ParseInt(string(parts[1]), 10, 64) if err != nil { diff --git a/pkg/util/netutil/net.go b/pkg/util/netutil/net.go index 3a852fa064dd..aad4514fd561 100644 --- a/pkg/util/netutil/net.go +++ b/pkg/util/netutil/net.go @@ -54,7 +54,7 @@ func ListenAndServeGRPC( return ln, nil } -var httpLogger = log.NewStdLogger(log.Severity_ERROR, "httpLogger") +var httpLogger = log.NewStdLogger(log.Severity_ERROR, "net/http") // Server is a thin wrapper around http.Server. See MakeServer for more detail. type Server struct {