open-nomad/command/agent/http_stdlog.go
Mahmood Ali 4e1366f285 agent: route http logs through hclog
Pipe http server log to hclog, so that it uses the same logging format
as rest of nomad logs.  Also, supports emitting them as json logs, when
json formatting is set.

The http server logs are emitted as Trace level, as they are typically
repsent HTTP client errors (e.g. failed tls handshakes, invalid headers,
etc).

Though, Panic logs represent server errors and are relayed as Error
level.
2020-04-20 10:33:40 -04:00

34 lines
840 B
Go

package agent
import (
"bytes"
"log"
hclog "github.com/hashicorp/go-hclog"
)
func newHTTPServerLogger(logger hclog.Logger) *log.Logger {
return log.New(&httpServerLoggerAdapter{logger}, "", 0)
}
// a logger adapter that forwards http server logs as a Trace level
// hclog log entries. Logs related to panics are forwarded with Error level.
//
// HTTP server logs are typically spurious as they represent HTTP
// client errors (e.g. TLS handshake failures).
type httpServerLoggerAdapter struct {
logger hclog.Logger
}
func (l *httpServerLoggerAdapter) Write(data []byte) (int, error) {
if bytes.Contains(data, []byte("panic")) {
str := string(bytes.TrimRight(data, " \t\n"))
l.logger.Error(str)
} else if l.logger.IsTrace() {
str := string(bytes.TrimRight(data, " \t\n"))
l.logger.Trace(str)
}
return len(data), nil
}