Merge pull request #1660 from alileza/add_http_errorlog

Log HTTP server errors using common/log
This commit is contained in:
Julius Volz 2016-05-26 15:03:15 -05:00
commit 194a486c60
3 changed files with 23 additions and 4 deletions

View File

@ -16,6 +16,7 @@ package log
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"net/url" "net/url"
"os" "os"
"runtime" "runtime"
@ -302,3 +303,16 @@ func Fatalln(args ...interface{}) {
func Fatalf(format string, args ...interface{}) { func Fatalf(format string, args ...interface{}) {
baseLogger.sourced().Fatalf(format, args...) baseLogger.sourced().Fatalf(format, args...)
} }
type errorLogWriter struct{}
func (errorLogWriter) Write(b []byte) (int, error) {
baseLogger.sourced().Error(string(b))
return len(b), nil
}
// NewErrorLogger returns a log.Logger that is meant to be used
// in the ErrorLog field of an http.Server to log HTTP server errors.
func NewErrorLogger() *log.Logger {
return log.New(&errorLogWriter{}, "", 0)
}

6
vendor/vendor.json vendored
View File

@ -173,10 +173,10 @@
"revisionTime": "2016-05-19T16:20:33Z" "revisionTime": "2016-05-19T16:20:33Z"
}, },
{ {
"checksumSHA1": "koBNYQryxAG8hyHBlpn8pcnSVdM=", "checksumSHA1": "qHoBp/PVBcLedTNZrF3toV9QGa0=",
"path": "github.com/prometheus/common/log", "path": "github.com/prometheus/common/log",
"revision": "c16e34897a744c32f6733ee720e60c4de13887fb", "revision": "a6ab08426bb262e2d190097751f5cfd1cfdfd17d",
"revisionTime": "2016-05-19T16:20:33Z" "revisionTime": "2016-05-26T15:55:09Z"
}, },
{ {
"checksumSHA1": "Zgmg/aOfoCNTAMtrXqBJmt852t0=", "checksumSHA1": "Zgmg/aOfoCNTAMtrXqBJmt852t0=",

View File

@ -239,7 +239,12 @@ func (h *Handler) Reload() <-chan struct{} {
// Run serves the HTTP endpoints. // Run serves the HTTP endpoints.
func (h *Handler) Run() { func (h *Handler) Run() {
log.Infof("Listening on %s", h.options.ListenAddress) log.Infof("Listening on %s", h.options.ListenAddress)
h.listenErrCh <- http.ListenAndServe(h.options.ListenAddress, h.router) server := &http.Server{
Addr: h.options.ListenAddress,
Handler: h.router,
ErrorLog: log.NewErrorLogger(),
}
h.listenErrCh <- server.ListenAndServe()
} }
func (h *Handler) alerts(w http.ResponseWriter, r *http.Request) { func (h *Handler) alerts(w http.ResponseWriter, r *http.Request) {