feat: base the lib on rs/zerolog/hlog
i discovered similarities in hlog. so i refactored this lib to use hlog to be more compatible. logginghandler.Handler setups all Handlers from hlog that which has similarities. only the proto handler is missing. a PR is open for hlog.
This commit is contained in:
parent
c5afa55bc7
commit
17fcd55b85
@ -5,39 +5,42 @@ package logginghandler
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/hlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetUUID gets the requests UUID from a request.
|
// GetUUID gets the requests UUID from a request.
|
||||||
func GetUUID(r *http.Request) string {
|
func GetUUID(r *http.Request) string {
|
||||||
return r.Header.Get("X-Request-ID")
|
uuid, ok := hlog.IDFromRequest(r)
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return uuid.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logger returns a logger with the UUID set.
|
// Logger returns a logger with the UUID set.
|
||||||
func Logger(r *http.Request) zerolog.Logger {
|
func Logger(r *http.Request) zerolog.Logger {
|
||||||
logger := log.With().Str("uuid", GetUUID(r)).Logger()
|
return *hlog.FromRequest(r)
|
||||||
|
|
||||||
return logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler is the http middleware handler.
|
// Handler sets up all the logging.
|
||||||
func Handler(next http.Handler) http.Handler {
|
func Handler(logger zerolog.Logger) func(http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return func(next http.Handler) http.Handler {
|
||||||
uuid := uuid.New().String()
|
return hlog.NewHandler(logger)(
|
||||||
r.Header.Set("X-Request-ID", uuid)
|
hlog.RemoteAddrHandler("remote")(
|
||||||
logger := Logger(r)
|
hlog.UserAgentHandler("user-agent")(
|
||||||
logger.Info().
|
hlog.RefererHandler("referer")(
|
||||||
Str("method", r.Method).
|
hlog.MethodHandler("method")(
|
||||||
Str("user-agent", r.UserAgent()).
|
hlog.RequestIDHandler("uuid", "X-Request-ID")(
|
||||||
Str("proto", r.Proto).
|
hlog.URLHandler("request-url")(
|
||||||
Str("referer", r.Referer()).
|
next,
|
||||||
Str("request-url", r.URL.String()).
|
),
|
||||||
Str("remote", r.RemoteAddr).
|
),
|
||||||
Msg("")
|
),
|
||||||
|
),
|
||||||
w.Header().Set("X-Request-ID", uuid)
|
),
|
||||||
next.ServeHTTP(w, r)
|
),
|
||||||
})
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,13 +12,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Example() {
|
func Example() {
|
||||||
handler := logginghandler.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
logger := log.With().Logger()
|
||||||
|
|
||||||
|
handler := logginghandler.Handler(logger)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
logger := logginghandler.Logger(r)
|
||||||
|
|
||||||
|
logger.Info().Msg("this is a request")
|
||||||
|
}))
|
||||||
|
|
||||||
http.Handle("/", handler)
|
http.Handle("/", handler)
|
||||||
log.Fatal().Msg(http.ListenAndServe(":5000", nil).Error())
|
log.Fatal().Msg(http.ListenAndServe(":5000", nil).Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHandler(w http.ResponseWriter, r *http.Request) {
|
func testHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Print("got request")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUUID(t *testing.T) {
|
func TestUUID(t *testing.T) {
|
||||||
@ -28,10 +34,9 @@ func TestUUID(t *testing.T) {
|
|||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
handler := logginghandler.Handler(http.HandlerFunc(testHandler))
|
handler := logginghandler.Handler(log.With().Logger())(http.HandlerFunc(testHandler))
|
||||||
|
|
||||||
handler.ServeHTTP(rr, req)
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
assert.NotEmpty(rr.Header().Get("X-Request-ID"))
|
assert.NotEmpty(rr.Header().Get("X-Request-ID"))
|
||||||
log.Print(rr.Header())
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user