-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.go
57 lines (48 loc) · 1.27 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
package middleware
import (
"fmt"
"github.com/dmitry-kostin/go-rest/src/pkg"
"net/http"
"time"
)
type StatusCodeResponseWriter struct {
http.ResponseWriter
statusCode int
}
func (s *StatusCodeResponseWriter) Status() int {
return s.statusCode
}
func (s *StatusCodeResponseWriter) WriteHeader(code int) {
s.statusCode = code
s.ResponseWriter.WriteHeader(code)
}
type logEntry struct {
Status int `json:"status"`
Duration time.Duration `json:"duration"`
Hostname string `json:"hostname"`
Method string `json:"method"`
Path string `json:"path"`
}
func (s logEntry) String() string {
return fmt.Sprintf("%d | \t %s | %s | %s %s", s.Status, s.Duration, s.Hostname, s.Method, s.Path)
}
type Logger struct {
*pkg.Logger
}
func NewLogger(logger *pkg.Logger) *Logger {
return &Logger{logger}
}
func (s *Logger) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
start := time.Now()
statusCodeRW := &StatusCodeResponseWriter{rw, http.StatusOK}
next.ServeHTTP(statusCodeRW, r)
s.Debug().Msgf("[request] %v", &logEntry{
Status: statusCodeRW.statusCode,
Duration: time.Since(start),
Hostname: r.Host,
Method: r.Method,
Path: r.URL.Path,
})
})
}