Skip to content

Commit 81fc293

Browse files
committed
introduce router log handler v2
1 parent 62a6717 commit 81fc293

File tree

16 files changed

+402
-68
lines changed

16 files changed

+402
-68
lines changed

cmd/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func runWeb(ctx *cli.Context) error {
8888
}
8989
defer func() {
9090
if panicked := recover(); panicked != nil {
91-
log.Fatal("PANIC: %v\n%s", panicked, string(log.Stack(2)))
91+
log.Fatal("PANIC: %v\n%s", panicked, log.Stack(2))
9292
}
9393
}()
9494

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@ ROUTER = console
459459
;; The level at which the router logs
460460
;ROUTER_LOG_LEVEL = Info
461461
;;
462+
;; The handler for router logs, it controls the log format
463+
;ROUTER_LOG_HANDLER = v2
464+
;;
462465
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
463466
;;
464467
;; Access Logger (Creates log in NCSA common log format)

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,8 @@ Default templates for project boards:
704704
- `MODE`: **console**: Logging mode. For multiple modes, use a comma to separate values. You can configure each mode in per mode log subsections `\[log.modename\]`. By default the file mode will log to `$ROOT_PATH/gitea.log`.
705705
- `LEVEL`: **Info**: General log level. \[Trace, Debug, Info, Warn, Error, Critical, Fatal, None\]
706706
- `STACKTRACE_LEVEL`: **None**: Default log level at which to log create stack traces. \[Trace, Debug, Info, Warn, Error, Critical, Fatal, None\]
707-
- `ROUTER_LOG_LEVEL`: **Info**: The log level that the router should log at. (If you are setting the access log, its recommended to place this at Debug.)
707+
- `ROUTER_LOG_LEVEL`: **Info**: The log level that the router should log at. (If you are setting the access log, it's recommended to set this to Debug.)
708+
- `ROUTER_LOG_HANDLER`: **v2**: The log handler that controls the log output format. Before 1.16 the router logs are outputted by handler `v1`. From 1.16, the default handler is `v2` which is more meaningful and friendly.
708709
- `ROUTER`: **console**: The mode or name of the log the router should log to. (If you set this to `,` it will log to default gitea logger.)
709710
NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take effect. Configure each mode in per mode log subsections `\[log.modename.router\]`.
710711
- `ENABLE_ACCESS_LOG`: **false**: Creates an access.log in NCSA common log format, or as per the following template

docs/content/doc/advanced/logging-documentation.en-us.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ changed if desired by setting the `ROUTER_LOG_LEVEL` value.
7979
Please note, setting the `LEVEL` of this logger to a level above
8080
`ROUTER_LOG_LEVEL` will result in no router logs.
8181

82+
You can control the output format by setting a log handler to `ROUTER_LOG_HANDLER`.
83+
Now Gitea has two log handlers:
84+
* `v1` is the default handler for Gitea before 1.16
85+
* `v2` is the default handler for Gitea from 1.16, it's more meaningful and friendly.
86+
87+
If you have applications depending on the log format (eg: fail2ban), please make sure you use the correct log handler and log format.
88+
8289
Each output sublogger for this logger is configured in
8390
`[log.sublogger.router]` sections. There are certain default values
8491
which will not be inherited from the `[log]` or relevant

modules/markup/markdown/markdown.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func actualRender(ctx *markup.RenderContext, input io.Reader, output io.Writer)
181181

182182
log.Warn("Unable to render markdown due to panic in goldmark: %v", err)
183183
if log.IsDebug() {
184-
log.Debug("Panic in markdown: %v\n%s", err, string(log.Stack(2)))
184+
log.Debug("Panic in markdown: %v\n%s", err, log.Stack(2))
185185
}
186186
_ = lw.CloseWithError(fmt.Errorf("%v", err))
187187
}()
@@ -214,7 +214,7 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
214214

215215
log.Warn("Unable to render markdown due to panic in goldmark - will return sanitized raw bytes")
216216
if log.IsDebug() {
217-
log.Debug("Panic in markdown: %v\n%s", err, string(log.Stack(2)))
217+
log.Debug("Panic in markdown: %v\n%s", err, log.Stack(2))
218218
}
219219
ret := markup.SanitizeReader(input, "")
220220
_, err = io.Copy(output, ret)

modules/public/public.go

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ type Options struct {
2323
CorsHandler func(http.Handler) http.Handler
2424
}
2525

26-
// AssetsHandler implements the static handler for serving custom or original assets.
27-
func AssetsHandler(opts *Options) func(next http.Handler) http.Handler {
26+
// AssetsURLPathPrefix is the path prefix for static asset files
27+
const AssetsURLPathPrefix = "/assets/"
28+
29+
// AssetsHandlerFunc implements the static handler for serving custom or original assets.
30+
func AssetsHandlerFunc(opts *Options) http.HandlerFunc {
2831
var custPath = filepath.Join(setting.CustomPath, "public")
2932
if !filepath.IsAbs(custPath) {
3033
custPath = filepath.Join(setting.AppWorkPath, custPath)
@@ -36,52 +39,46 @@ func AssetsHandler(opts *Options) func(next http.Handler) http.Handler {
3639
opts.Prefix += "/"
3740
}
3841

39-
return func(next http.Handler) http.Handler {
40-
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
41-
if !strings.HasPrefix(req.URL.Path, opts.Prefix) {
42-
next.ServeHTTP(resp, req)
43-
return
44-
}
45-
if req.Method != "GET" && req.Method != "HEAD" {
46-
resp.WriteHeader(http.StatusNotFound)
47-
return
48-
}
49-
50-
file := req.URL.Path
51-
file = file[len(opts.Prefix):]
52-
if len(file) == 0 {
53-
resp.WriteHeader(http.StatusNotFound)
54-
return
55-
}
56-
if strings.Contains(file, "\\") {
57-
resp.WriteHeader(http.StatusBadRequest)
58-
return
59-
}
60-
file = "/" + file
61-
62-
var written bool
63-
if opts.CorsHandler != nil {
64-
written = true
65-
opts.CorsHandler(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
66-
written = false
67-
})).ServeHTTP(resp, req)
68-
}
69-
if written {
70-
return
71-
}
72-
73-
// custom files
74-
if opts.handle(resp, req, http.Dir(custPath), file) {
75-
return
76-
}
77-
78-
// internal files
79-
if opts.handle(resp, req, fileSystem(opts.Directory), file) {
80-
return
81-
}
42+
return func(resp http.ResponseWriter, req *http.Request) {
43+
if req.Method != "GET" && req.Method != "HEAD" {
44+
resp.WriteHeader(http.StatusNotFound)
45+
return
46+
}
8247

48+
file := req.URL.Path
49+
file = file[len(opts.Prefix):]
50+
if len(file) == 0 {
8351
resp.WriteHeader(http.StatusNotFound)
84-
})
52+
return
53+
}
54+
if strings.Contains(file, "\\") {
55+
resp.WriteHeader(http.StatusBadRequest)
56+
return
57+
}
58+
file = "/" + file
59+
60+
var written bool
61+
if opts.CorsHandler != nil {
62+
written = true
63+
opts.CorsHandler(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
64+
written = false
65+
})).ServeHTTP(resp, req)
66+
}
67+
if written {
68+
return
69+
}
70+
71+
// custom files
72+
if opts.handle(resp, req, http.Dir(custPath), file) {
73+
return
74+
}
75+
76+
// internal files
77+
if opts.handle(resp, req, fileSystem(opts.Directory), file) {
78+
return
79+
}
80+
81+
resp.WriteHeader(http.StatusNotFound)
8582
}
8683
}
8784

modules/setting/log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func getStacktraceLogLevel(section *ini.Section, key string, defaultValue string
126126

127127
func generateLogConfig(sec *ini.Section, name string, defaults defaultLogOptions) (mode, jsonConfig, levelName string) {
128128
level := getLogLevel(sec, "LEVEL", LogLevel)
129+
levelName = level.String()
129130
stacktraceLevelName := getStacktraceLogLevel(sec, "STACKTRACE_LEVEL", StacktraceLogLevel)
130131
stacktraceLevel := log.FromString(stacktraceLevelName)
131132
mode = name

modules/setting/setting.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ var (
332332
LogRootPath string
333333
DisableRouterLog bool
334334
RouterLogLevel log.Level
335+
RouterLogHandler string
335336
EnableAccessLog bool
336337
EnableSSHLog bool
337338
AccessLogTemplate string
@@ -579,6 +580,7 @@ func NewContext() {
579580
LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(AppWorkPath, "log"))
580581
forcePathSeparator(LogRootPath)
581582
RouterLogLevel = log.FromString(Cfg.Section("log").Key("ROUTER_LOG_LEVEL").MustString("Info"))
583+
RouterLogHandler = Cfg.Section("log").Key("ROUTER_LOG_HANDLER").MustString("v2")
582584

583585
sec := Cfg.Section("server")
584586
AppName = Cfg.Section("").Key("APP_NAME").MustString("Gitea: Git with a cup of tea")

modules/web/route.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"code.gitea.io/gitea/modules/context"
1515
"code.gitea.io/gitea/modules/web/middleware"
16+
"code.gitea.io/gitea/routers/common"
1617

1718
"gitea.com/go-chi/binding"
1819
chi "github.com/go-chi/chi/v5"
@@ -40,6 +41,7 @@ func Wrap(handlers ...interface{}) http.HandlerFunc {
4041
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
4142
for i := 0; i < len(handlers); i++ {
4243
handler := handlers[i]
44+
common.UpdateContextHandlerFuncInfo(req.Context(), handler)
4345
switch t := handler.(type) {
4446
case http.HandlerFunc:
4547
t(resp, req)

routers/common/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"code.gitea.io/gitea/modules/log"
1313
)
1414

15-
// LoggerHandler is a handler that will log the routing to the default gitea log
16-
func LoggerHandler(level log.Level) func(next http.Handler) http.Handler {
15+
// NewLoggerHandlerV1 is a handler that will log the routing to the default gitea log
16+
func NewLoggerHandlerV1(level log.Level) func(next http.Handler) http.Handler {
1717
return func(next http.Handler) http.Handler {
1818
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
1919
start := time.Now()

0 commit comments

Comments
 (0)