Skip to content

Commit

Permalink
util/{log,netutil}: fix the log bridge
Browse files Browse the repository at this point in the history
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
  • Loading branch information
knz committed Oct 7, 2020
1 parent 21ff031 commit 9b5e5a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions pkg/util/log/log_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/netutil/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9b5e5a4

Please sign in to comment.