diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-01-20 09:47:43 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-19 20:47:43 -0500 |
commit | 135b0e502d93c591b21083794dc100b53e520477 (patch) | |
tree | 591f5034aa021f97e71be94d97f58a45c014ddf7 /routers/routes | |
parent | 41e19b93a27318a1f8b88ee76837a30969ccb767 (diff) | |
download | gitea-135b0e502d93c591b21083794dc100b53e520477.tar.gz gitea-135b0e502d93c591b21083794dc100b53e520477.zip |
Fix log http status is always zero (#14400)
* Fix log http status is always zero
* Fix lint
Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'routers/routes')
-rw-r--r-- | routers/routes/chi.go | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/routers/routes/chi.go b/routers/routes/chi.go index 16fd6ea905..2400140ae6 100644 --- a/routers/routes/chi.go +++ b/routers/routes/chi.go @@ -16,6 +16,7 @@ import ( "text/template" "time" + "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/metrics" @@ -90,9 +91,11 @@ func LoggerHandler(level log.Level) func(next http.Handler) http.Handler { next.ServeHTTP(w, req) - ww := middleware.NewWrapResponseWriter(w, req.ProtoMajor) + var status int + if v, ok := w.(context.ResponseWriter); ok { + status = v.Status() + } - status := ww.Status() _ = log.GetLogger("router").Log(0, level, "Completed %s %s %v %s in %v", log.ColoredMethod(req.Method), req.URL.RequestURI(), log.ColoredStatus(status), log.ColoredStatus(status, http.StatusText(status)), log.ColoredTime(time.Since(start))) }) } @@ -183,6 +186,11 @@ var ( // NewChi creates a chi Router func NewChi() chi.Router { c := chi.NewRouter() + c.Use(func(next http.Handler) http.Handler { + return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { + next.ServeHTTP(context.NewResponse(resp), req) + }) + }) c.Use(middleware.RealIP) if !setting.DisableRouterLog && setting.RouterLogLevel != log.NONE { if log.GetLogger("router").GetLevel() <= setting.RouterLogLevel { |