@@ -87,8 +87,21 @@ func mwStripTrailingSlash(next http.Handler) http.Handler {
87
87
})
88
88
}
89
89
90
+ type StatusRecorder struct {
91
+ http.ResponseWriter
92
+ Status int
93
+ }
94
+
95
+ func (r * StatusRecorder ) WriteHeader (status int ) {
96
+ r .Status = status
97
+ r .ResponseWriter .WriteHeader (status )
98
+ }
99
+
90
100
func (a * app ) mwStructLogger (next http.Handler ) http.Handler {
91
101
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
102
+ record := & StatusRecorder {ResponseWriter : w , Status : http .StatusOK }
103
+ next .ServeHTTP (record , r )
104
+
92
105
scheme := "http"
93
106
if r .TLS != nil {
94
107
scheme = "https"
@@ -101,33 +114,47 @@ func (a *app) mwStructLogger(next http.Handler) http.Handler {
101
114
Str ("url" , url ).
102
115
Str ("method" , r .Method ).
103
116
Str ("remote_addr" , r .RemoteAddr ).
104
- Msgf ( "[%s] %s " , r . Method , url )
105
- next . ServeHTTP ( w , r )
117
+ Int ( "status " , record . Status ).
118
+ Msg ( url )
106
119
})
107
120
}
108
121
109
122
func (a * app ) mwSummaryLogger (next http.Handler ) http.Handler {
110
- bold := func (s string ) string {
111
- return "\033 [1m" + s + "\033 [0m"
112
- }
113
-
114
- pink := func (s string ) string {
115
- return "\033 [35m" + s + "\033 [0m"
116
- }
117
-
118
- aqua := func (s string ) string {
119
- return "\033 [36m" + s + "\033 [0m"
123
+ bold := func (s string ) string { return "\033 [1m" + s + "\033 [0m" }
124
+ orange := func (s string ) string { return "\033 [33m" + s + "\033 [0m" }
125
+ aqua := func (s string ) string { return "\033 [36m" + s + "\033 [0m" }
126
+ red := func (s string ) string { return "\033 [31m" + s + "\033 [0m" }
127
+ green := func (s string ) string { return "\033 [32m" + s + "\033 [0m" }
128
+
129
+ fmtCode := func (code int ) string {
130
+ switch {
131
+ case code >= 500 :
132
+ return red (fmt .Sprintf ("%d" , code ))
133
+ case code >= 400 :
134
+ return orange (fmt .Sprintf ("%d" , code ))
135
+ case code >= 300 :
136
+ return aqua (fmt .Sprintf ("%d" , code ))
137
+ default :
138
+ return green (fmt .Sprintf ("%d" , code ))
139
+ }
120
140
}
121
141
122
142
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
143
+ record := & StatusRecorder {ResponseWriter : w , Status : http .StatusOK }
144
+ next .ServeHTTP (record , r ) // Blocks until the next handler returns.
145
+
123
146
scheme := "http"
124
147
if r .TLS != nil {
125
148
scheme = "https"
126
149
}
127
150
128
151
url := fmt .Sprintf ("%s://%s%s %s" , scheme , r .Host , r .RequestURI , r .Proto )
129
152
130
- log .Info ().Msgf ("%s %s" , bold (pink ("[" + r .Method + "]" )), aqua (url ))
131
- next .ServeHTTP (w , r )
153
+ log .Info ().
154
+ Msgf ("%s %s %s" ,
155
+ bold (orange ("" + r .Method + "" )),
156
+ aqua (url ),
157
+ bold (fmtCode (record .Status )),
158
+ )
132
159
})
133
160
}
0 commit comments