4 回答

TA貢獻1852條經驗 獲得超1個贊
這種方法是可行的。
type loggingResponseWriter struct {
http.ResponseWriter
statusCode int
}
func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
return &loggingResponseWriter{w, http.StatusOK}
}
func (lrw *loggingResponseWriter) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}
func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
log.Printf("--> %s %s", req.Method, req.URL.Path)
lrw := NewLoggingResponseWriter(w)
wrappedHandler.ServeHTTP(lrw, req)
statusCode := lrw.statusCode
log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))
})
}

TA貢獻1818條經驗 獲得超3個贊
使用內格羅尼。它的工作原理與@huangapple 的答案相同,但處理程序實際上實現了所有接口。
import (
"github.com/urfave/negroni"
)
func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
log.Printf("--> %s %s", req.Method, req.URL.Path)
lrw := negroni.NewResponseWriter(w)
wrappedHandler.ServeHTTP(lrw, req)
statusCode := lrw.Status()
log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))
})
}

TA貢獻1845條經驗 獲得超8個贊
在我的例子中,我不使用外部庫,我不想包裝http.ResponseWriter. 我必須在請求的上下文中添加響應的狀態,以便以后在日志記錄中使用。所以我創建了一個小幫手來同時在 ResponseWriter 和請求的上下文中寫入狀態。
type AppContext string
var StatusCode = AppContext("statuCode")
func WriteHeaderAndContext(w http.ResponseWriter, statusCode int, r *http.Request) {
ctx := context.WithValue(r.Context(), StatusCode, statusCode)
*r = *(r.WithContext(ctx))
w.WriteHeader(statusCode)
}
在日志記錄中,我將值檢索為
r.Context().Value(StatusCode)
缺點發生在被叫方,看起來有點不尋常。例如
WriteHeaderAndContext(w, http.StatusCreated, r)
我們通常做的地方
w.WriteHeader(http.StatusCreated)
- 4 回答
- 0 關注
- 329 瀏覽
添加回答
舉報