-
Notifications
You must be signed in to change notification settings - Fork 0
/
logrequest.go
119 lines (101 loc) · 3.21 KB
/
logrequest.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package logrequest
import (
"fmt"
"log"
"net/http"
"time"
)
// LogRequest struct
// Pass values from middleware to this struct
type LogRequest struct {
Writer http.ResponseWriter
Request *http.Request
Handler http.Handler
NewLine int
Timestamp bool
HideDuration bool
}
type statusWriter struct {
http.ResponseWriter
statusCode int
}
type RequestFields struct {
Method string
Url string
RemoteAddress string
Protocol string
Time time.Time
Duration time.Duration
StatusCode int
}
// ToLogger will print the Started and Completed request info to the passed logger
func (lr LogRequest) ToLogger(logger *log.Logger) {
if lr.Timestamp {
logger.Printf(`Started %s "%s" %s %s at %s`, lr.Request.Method, lr.Request.URL.RequestURI(), lr.Request.RemoteAddr, lr.Request.Proto, time.Now().Format("2006-01-02 15:04:05"))
} else {
logger.Printf(`Started %s "%s" %s %s`, lr.Request.Method, lr.Request.URL.RequestURI(), lr.Request.RemoteAddr, lr.Request.Proto)
}
if lr.HideDuration {
sw, _ := lr.parseRequest()
logger.Printf("Completed %d", sw.statusCode)
} else {
sw, completedDuration := lr.parseRequest()
logger.Printf("Completed %d in %s", sw.statusCode, completedDuration)
}
if lr.NewLine > 0 {
for i := 1; i <= lr.NewLine; i++ {
logger.Println("\t")
}
}
}
// ToString will return a map with the key 'started' and 'completed' that contain
// a string output for eacch
func (lr LogRequest) ToString() map[string]string {
sw, completedDuration := lr.parseRequest()
ts := make(map[string]string)
if lr.Timestamp {
ts["started"] = fmt.Sprintf(`Started %s "%s" %s %s at %s`, lr.Request.Method, lr.Request.URL.RequestURI(), lr.Request.RemoteAddr, lr.Request.Proto, time.Now().Format("2006-01-02 15:04:05"))
} else {
ts["started"] = fmt.Sprintf(`Started %s "%s" %s %s`, lr.Request.Method, lr.Request.URL.RequestURI(), lr.Request.RemoteAddr, lr.Request.Proto)
}
if lr.HideDuration {
ts["completed"] = fmt.Sprintf("Completed %d", sw.statusCode)
} else {
ts["completed"] = fmt.Sprintf("Completed %d in %s", sw.statusCode, completedDuration)
}
return ts
}
// ToFields returns a RequestFields struct which contains each field that is
// used in the request.
func (lr LogRequest) ToFields() RequestFields {
sw, completedDuration := lr.parseRequest()
rf := RequestFields{}
rf.Method = lr.Request.Method
rf.Url = lr.Request.URL.RequestURI()
rf.RemoteAddress = lr.Request.RemoteAddr
rf.Protocol = lr.Request.Proto
rf.Time = time.Now()
rf.Duration = completedDuration
rf.StatusCode = sw.statusCode
return rf
}
// parseRequest will time the request and retrieve the status from the
// ResponseWriter. Returns the statusWriter struct and the duration
// of the request.
func (lr LogRequest) parseRequest() (statusWriter, time.Duration) {
startTime := time.Now()
sw := statusWriter{ResponseWriter: lr.Writer}
lr.Handler.ServeHTTP(&sw, lr.Request)
return sw, time.Since(startTime)
}
func (w *statusWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func (w *statusWriter) Write(b []byte) (int, error) {
if w.statusCode == 0 {
w.statusCode = 200
}
n, err := w.ResponseWriter.Write(b)
return n, err
}