-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
78 lines (61 loc) · 1.61 KB
/
logger.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
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"runtime"
"time"
"github.com/gorilla/mux"
)
// #region Pre-recorded messages
const (
callFailure string = "Echec de l'appel"
callSuccess string = "Réussite de l'appel"
)
// #endregion Pre-recorded messages
func initMiddleware(router *mux.Router) {
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s\t%s\t", r.Method, r.RequestURI)
printRequest(r.RemoteAddr)
constructHeaders(&w, r)
next.ServeHTTP(w, r)
})
})
}
// LoggerHandler Middleware qui permet de logger les requêtes entrantes
func LoggerHandler(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
x, err := httputil.DumpRequest(r, true)
if err != nil {
failOnError(err, "An error occured during the request")
}
log.Printf("[BEGIN CALL] - %s\t%s\t", r.Method, r.RequestURI)
log.Println("[HEADER] - ", fmt.Sprintf("%q", x))
next.ServeHTTP(w, r)
log.Printf(
"[END CALL] - %s\t%s\t%s\t",
r.Method,
r.RequestURI,
time.Since(start),
)
w.WriteHeader(http.StatusOK)
})
}
func failOnError(err error, msg string) {
if err != nil {
log.Printf("[ERROR] - %s: %s", msg, err)
}
}
func printRequest(addr string) {
log.Printf("[ %s ] - Request from %s ", time.Now().Format(time.RFC3339), addr)
}
func trace(desc string) {
pc := make([]uintptr, 15)
n := runtime.Callers(2, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
log.Printf("%s - %s\n", frame.Function, desc)
}