Skip to content

Commit 4d3bbef

Browse files
committed
Correct the access log format
Remove backslashes from template string Add ctx.RemoteHost which contains a best-effort to strip the port from req.RemoteAddr, falling back to untouch if an error is encountered Default the first field of the access log to ctx.RemoteHost Adjust templates and documentation accordingly Signed-off-by: Gary Moon <[email protected]>
1 parent 2d91afa commit 4d3bbef

File tree

6 files changed

+13
-6
lines changed

6 files changed

+13
-6
lines changed

custom/conf/app.example.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ ROUTER = console
603603
;ACCESS = file
604604
;;
605605
;; Sets the template used to create the access log.
606-
;ACCESS_LOG_TEMPLATE = {{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"
606+
;ACCESS_LOG_TEMPLATE = {{.Ctx.RemoteHost}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}" "{{.Ctx.Req.UserAgent}}"
607607
;;
608608
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
609609
;;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ Default templates for project boards:
878878

879879
- `ENABLE_ACCESS_LOG`: **false**: Creates an access.log in NCSA common log format, or as per the following template
880880
- `ACCESS`: **file**: Logging mode for the access logger, use a comma to separate values. Configure each mode in per mode log subsections `\[log.modename.access\]`. By default the file mode will log to `$ROOT_PATH/access.log`. (If you set this to `,` it will log to the default Gitea logger.)
881-
- `ACCESS_LOG_TEMPLATE`: **`{{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"`**: Sets the template used to create the access log.
881+
- `ACCESS_LOG_TEMPLATE`: **`{{.Ctx.RemoteHost}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}" "{{.Ctx.Req.UserAgent}}"`**: Sets the template used to create the access log.
882882
- The following variables are available:
883883
- `Ctx`: the `context.Context` of the request.
884884
- `Identity`: the SignedUserName or `"-"` if not logged in.

docs/content/doc/administration/config-cheat-sheet.zh-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ test01.xls: application/vnd.ms-excel; charset=binary
265265
- `LEVEL`: 日志级别,默认为 `Trace`
266266
- `DISABLE_ROUTER_LOG`: 关闭日志中的路由日志。
267267
- `ENABLE_ACCESS_LOG`: 是否开启 Access Log, 默认为 false。
268-
- `ACCESS_LOG_TEMPLATE`: `access.log` 输出内容的模板,默认模板:**`{{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"`**
268+
- `ACCESS_LOG_TEMPLATE`: `access.log` 输出内容的模板,默认模板:**`{{.Ctx.RemoteHost}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}" "{{.Ctx.Req.UserAgent}}"`**
269269
模板支持以下参数:
270270
- `Ctx`: 请求上下文。
271271
- `Identity`: 登录用户名,默认: “`-`”。

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ log using the value: `ACCESS = ,`
304304

305305
This value represent a go template. It's default value is:
306306

307-
`{{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"`
307+
`{{.Ctx.RemoteHost}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}" "{{.Ctx.Req.UserAgent}}"`
308308

309309
The template is passed following options:
310310

modules/context/access_log.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"context"
99
"fmt"
10+
"net"
1011
"net/http"
1112
"strings"
1213
"text/template"
@@ -67,17 +68,23 @@ func AccessLogger() func(http.Handler) http.Handler {
6768
requestID = parseRequestIDFromRequestHeader(req)
6869
}
6970

71+
reqHost, _, err := net.SplitHostPort(req.RemoteAddr)
72+
if err != nil {
73+
reqHost = req.RemoteAddr
74+
}
75+
7076
next.ServeHTTP(w, r)
7177
rw := w.(ResponseWriter)
7278

7379
buf := bytes.NewBuffer([]byte{})
74-
err := logTemplate.Execute(buf, routerLoggerOptions{
80+
err = logTemplate.Execute(buf, routerLoggerOptions{
7581
req: req,
7682
Identity: &identity,
7783
Start: &start,
7884
ResponseWriter: rw,
7985
Ctx: map[string]interface{}{
8086
"RemoteAddr": req.RemoteAddr,
87+
"RemoteHost": reqHost,
8188
"Req": req,
8289
},
8390
RequestID: &requestID,

modules/setting/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func loadLogFrom(rootCfg ConfigProvider) {
152152
Log.EnableSSHLog = sec.Key("ENABLE_SSH_LOG").MustBool(false)
153153
Log.EnableAccessLog = sec.Key("ENABLE_ACCESS_LOG").MustBool(false)
154154
Log.AccessLogTemplate = sec.Key("ACCESS_LOG_TEMPLATE").MustString(
155-
`{{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"`,
155+
`{{.Ctx.RemoteHost}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}" "{{.Ctx.Req.UserAgent}}"`,
156156
)
157157
Log.RequestIDHeaders = sec.Key("REQUEST_ID_HEADERS").Strings(",")
158158
// the `MustString` updates the default value, and `log.ACCESS` is used by `generateNamedLogger("access")` later

0 commit comments

Comments
 (0)