Browse Source

Merge pull request '在请求入口处的日志中增加用户名的打印' (#592) from log-opt into V20211101

Reviewed-on: https://git.openi.org.cn/OpenI/aiforge/pulls/592
Reviewed-by: ychao_1983 <ychao_1983@sina.com>
pull/608/head
ychao_1983 3 years ago
parent
commit
e855409de1
2 changed files with 6 additions and 3 deletions
  1. +2
    -0
      modules/context/context.go
  2. +4
    -3
      routers/routes/routes.go

+ 2
- 0
modules/context/context.go View File

@@ -310,9 +310,11 @@ func Contexter() macaron.Handler {
ctx.Data["SignedUserID"] = ctx.User.ID
ctx.Data["SignedUserName"] = ctx.User.Name
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
c.Data["SignedUserName"] = ctx.User.Name
} else {
ctx.Data["SignedUserID"] = int64(0)
ctx.Data["SignedUserName"] = ""
c.Data["SignedUserName"] = ""
}

// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.


+ 4
- 3
routers/routes/routes.go View File

@@ -114,14 +114,14 @@ func RouterHandler(level log.Level) func(ctx *macaron.Context) {
}

// SetLogMsgID set msgID in Context
func SetLogMsgID() func(ctx *macaron.Context) {
func SetLogMsgID() macaron.Handler {
return func(ctx *macaron.Context) {
start := time.Now()

uuid := gouuid.NewV4().String()
ctx.Data["MsgID"] = uuid

log.Info("Started %s %s for %s", log.ColoredMethod(ctx.Req.Method), ctx.Req.URL.RequestURI(), ctx.RemoteAddr(), ctx.Data["MsgID"])
log.Info("%s Started %s %s for %s", ctx.Data["SignedUserName"], log.ColoredMethod(ctx.Req.Method), ctx.Req.URL.RequestURI(), ctx.RemoteAddr(), ctx.Data["MsgID"])

rw := ctx.Resp.(macaron.ResponseWriter)
ctx.Next()
@@ -149,7 +149,7 @@ func NewMacaron() *macaron.Macaron {
m.Use(macaron.Logger())
}
}
m.Use(SetLogMsgID())
//m.Use(SetLogMsgID())
// Access Logger is similar to Router Log but more configurable and by default is more like the NCSA Common Log format
if setting.EnableAccessLog {
setupAccessLogger(m)
@@ -257,6 +257,7 @@ func NewMacaron() *macaron.Macaron {
DisableDebug: !setting.EnablePprof,
}))
m.Use(context.Contexter())
m.Use(SetLogMsgID())
// OK we are now set-up enough to allow us to create a nicer recovery than
// the default macaron recovery
m.Use(context.Recovery())


Loading…
Cancel
Save