-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
middleware.go
39 lines (32 loc) · 1.01 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package daemon
import (
"net/http"
"time"
"github.com/sirupsen/logrus"
"github.com/urfave/negroni"
"github.com/ory/x/logrusx"
"github.com/ory/x/healthx"
"github.com/ory/x/reqlog"
)
func NewNegroniLoggerMiddleware(l *logrusx.Logger, name string) *reqlog.Middleware {
n := reqlog.NewMiddlewareFromLogger(l, name).ExcludePaths(healthx.AliveCheckPath, healthx.ReadyCheckPath)
n.Before = func(entry *logrusx.Logger, req *http.Request, remoteAddr string) *logrusx.Logger {
return entry.WithFields(logrus.Fields{
"name": name,
"request": req.RequestURI,
"method": req.Method,
"remote": remoteAddr,
})
}
n.After = func(entry *logrusx.Logger, req *http.Request, res negroni.ResponseWriter, latency time.Duration, name string) *logrusx.Logger {
return entry.WithFields(logrus.Fields{
"name": name,
"status": res.Status(),
"text_status": http.StatusText(res.Status()),
"took": latency,
})
}
return n
}