mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Refactor Router Logger (#17308)
Make router logger more friendly, show the related function name/file/line. [BREAKING] This PR substantially changes the logging format of the router logger. If you use this logging for monitoring e.g. fail2ban you will need to update this to match the new format.
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
)
|
||||
|
||||
// LoggerHandler is a handler that will log the routing to the default gitea log
|
||||
func LoggerHandler(level log.Level) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
start := time.Now()
|
||||
|
||||
_ = log.GetLogger("router").Log(0, level, "Started %s %s for %s", log.ColoredMethod(req.Method), req.URL.RequestURI(), req.RemoteAddr)
|
||||
|
||||
next.ServeHTTP(w, req)
|
||||
|
||||
var status int
|
||||
if v, ok := w.(context.ResponseWriter); ok {
|
||||
status = v.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)))
|
||||
})
|
||||
}
|
||||
}
|
@@ -13,6 +13,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/process"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/web/routing"
|
||||
|
||||
"github.com/chi-middleware/proxy"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
@@ -49,11 +50,10 @@ func Middlewares() []func(http.Handler) http.Handler {
|
||||
|
||||
handlers = append(handlers, middleware.StripSlashes)
|
||||
|
||||
if !setting.DisableRouterLog && setting.RouterLogLevel != log.NONE {
|
||||
if log.GetLogger("router").GetLevel() <= setting.RouterLogLevel {
|
||||
handlers = append(handlers, LoggerHandler(setting.RouterLogLevel))
|
||||
}
|
||||
if !setting.DisableRouterLog {
|
||||
handlers = append(handlers, routing.NewLoggerHandler())
|
||||
}
|
||||
|
||||
if setting.EnableAccessLog {
|
||||
handlers = append(handlers, context.AccessLogger())
|
||||
}
|
||||
@@ -63,10 +63,11 @@ func Middlewares() []func(http.Handler) http.Handler {
|
||||
// Why we need this? The Recovery() will try to render a beautiful
|
||||
// error page for user, but the process can still panic again, and other
|
||||
// middleware like session also may panic then we have to recover twice
|
||||
// and send a simple error page that should not panic any more.
|
||||
// and send a simple error page that should not panic anymore.
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2)))
|
||||
routing.UpdatePanicError(req.Context(), err)
|
||||
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, log.Stack(2))
|
||||
log.Error("%v", combinedErr)
|
||||
if setting.IsProd {
|
||||
http.Error(resp, http.StatusText(500), 500)
|
||||
|
Reference in New Issue
Block a user