forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
66 lines (52 loc) · 1.55 KB
/
logging.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package httptools
import (
"net/http"
"net/http/httputil"
"time"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
func RequestLogger(label string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lw := newLoggingResponseWriter(w)
h.ServeHTTP(lw, r)
method := r.Method
fullPath := r.RequestURI
if fullPath == "" {
fullPath = r.URL.RequestURI()
}
proto := r.Proto
responseCode := lw.status
duration := time.Since(lw.start).Nanoseconds() / int64(time.Millisecond)
log.Infof("%s: %s %s %s %d %d", label, method, fullPath, proto, responseCode, duration)
})
}
type loggingResponseWriter struct {
http.ResponseWriter
status int
start time.Time
}
func newLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
return &loggingResponseWriter{ResponseWriter: w, start: time.Now()}
}
func (w *loggingResponseWriter) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
w.status = statusCode
}
func ContextLogger(r *http.Request) *log.Entry {
return log.WithField("application", mux.Vars(r)["application"])
}
func ContextLoggerWithId(r *http.Request) *log.Entry {
reName := mux.Vars(r)["application"]
serviceId := mux.Vars(r)["serviceId"]
fields := map[string]interface{}{"application": reName, "service ID": serviceId}
return log.WithFields(fields)
}
func DumpRequestToLog(r *http.Request, logger *log.Entry) {
b, err := httputil.DumpRequest(r, false)
if err != nil {
logger.Errorf("Failed to log request")
return
}
logger.Infof("%s", b)
}