mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
extract log entry from context in pkg/download (#874)
* extract log entry from context in pkg/download * update doc for LogEntryHandler
This commit is contained in:
committed by
Michal Pristas
parent
9f8e7bc145
commit
bc35636749
+5
-11
@@ -5,7 +5,6 @@ import (
|
||||
"github.com/gobuffalo/buffalo/render"
|
||||
"github.com/gomods/athens/pkg/log"
|
||||
"github.com/gomods/athens/pkg/middleware"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ProtocolHandler is a function that takes all that it needs to return
|
||||
@@ -20,18 +19,13 @@ type HandlerOpts struct {
|
||||
Engine *render.Engine
|
||||
}
|
||||
|
||||
// LogEntryHandler constructs a log.Entry out of the given
|
||||
// *log.Logger so that it applies default fields to every single
|
||||
// incoming request without having to do those in every single handler.
|
||||
// This is like a middleware minus the context magic.
|
||||
// LogEntryHandler pulls a log entry from the buffalo context. Thanks to the
|
||||
// LogEntryMiddleware, we should have a log entry stored in the context for each
|
||||
// request with request-specific fields. This will grab the entry and pass it to
|
||||
// the protocol handlers
|
||||
func LogEntryHandler(ph ProtocolHandler, opts *HandlerOpts) buffalo.Handler {
|
||||
return func(c buffalo.Context) error {
|
||||
req := c.Request()
|
||||
ent := opts.Logger.WithFields(logrus.Fields{
|
||||
"http-method": req.Method,
|
||||
"http-path": req.URL.Path,
|
||||
"http-url": req.URL.String(),
|
||||
})
|
||||
ent := log.EntryFromContext(c)
|
||||
handler := ph(opts.Protocol, ent, opts.Engine)
|
||||
|
||||
return handler(c)
|
||||
|
||||
Reference in New Issue
Block a user