From 19511ebfa857b50255a347eae1ffbb970bdcc9be Mon Sep 17 00:00:00 2001 From: lewis <747342561@qq.com> Date: Fri, 22 Oct 2021 11:49:09 +0800 Subject: [PATCH 1/2] add username in log start --- routers/routes/routes.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 892368315..127c034ca 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -113,14 +113,21 @@ func RouterHandler(level log.Level) func(ctx *macaron.Context) { } // SetLogMsgID set msgID in Context -func SetLogMsgID() func(ctx *macaron.Context) { - return func(ctx *macaron.Context) { +func SetLogMsgID() macaron.Handler { + return func(ctx *macaron.Context, sess session.Store) { 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"]) + // Get user from session if logged in. + user, _ := auth.SignedInUser(ctx, sess) + var username string + if user != nil { + username = user.Name + } + + log.Info("%s Started %s %s for %s", username, log.ColoredMethod(ctx.Req.Method), ctx.Req.URL.RequestURI(), ctx.RemoteAddr(), ctx.Data["MsgID"]) rw := ctx.Resp.(macaron.ResponseWriter) ctx.Next() @@ -148,7 +155,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) @@ -256,6 +263,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()) From e8a73ab6fb270a62a26b5e6e4c9839195418402f Mon Sep 17 00:00:00 2001 From: lewis <747342561@qq.com> Date: Mon, 25 Oct 2021 10:09:44 +0800 Subject: [PATCH 2/2] add username in log --- modules/context/context.go | 2 ++ routers/routes/routes.go | 11 ++--------- 2 files changed, 4 insertions(+), 9 deletions(-) mode change 100644 => 100755 modules/context/context.go diff --git a/modules/context/context.go b/modules/context/context.go old mode 100644 new mode 100755 index 71c8986fb..6877780e3 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -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. diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 546268a82..f6e4c9a72 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -115,20 +115,13 @@ func RouterHandler(level log.Level) func(ctx *macaron.Context) { // SetLogMsgID set msgID in Context func SetLogMsgID() macaron.Handler { - return func(ctx *macaron.Context, sess session.Store) { + return func(ctx *macaron.Context) { start := time.Now() uuid := gouuid.NewV4().String() ctx.Data["MsgID"] = uuid - // Get user from session if logged in. - user, _ := auth.SignedInUser(ctx, sess) - var username string - if user != nil { - username = user.Name - } - - log.Info("%s Started %s %s for %s", username, 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()