diff options
author | zeripath <art27@cantab.net> | 2019-02-06 03:06:41 +0000 |
---|---|---|
committer | techknowlogick <matti@mdranta.net> | 2019-02-05 22:06:41 -0500 |
commit | f286a5abb42f94d7c597f41b25990f2ccee0bb77 (patch) | |
tree | 3fe2d144b63b088e38a8bbd3ff64965edb3e1183 /routers | |
parent | 3b7f41f9f7bbd3d19ce5a6b667b5c1f56c2b1b51 (diff) | |
download | gitea-f286a5abb42f94d7c597f41b25990f2ccee0bb77.tar.gz gitea-f286a5abb42f94d7c597f41b25990f2ccee0bb77.zip |
Allow Macaron to be set to log through to gitea.log (#5667)
* Allow Macaron to be set to log through gitea.log
Fix #4291
Diffstat (limited to 'routers')
-rw-r--r-- | routers/routes/routes.go | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 48d1dc6a7f..58274626c0 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -6,6 +6,7 @@ package routes import ( "encoding/gob" + "fmt" "net/http" "os" "path" @@ -45,12 +46,34 @@ import ( macaron "gopkg.in/macaron.v1" ) +func giteaLogger(l *log.LoggerAsWriter) macaron.Handler { + return func(ctx *macaron.Context) { + start := time.Now() + + l.Log(fmt.Sprintf("[Macaron] Started %s %s for %s", ctx.Req.Method, ctx.Req.RequestURI, ctx.RemoteAddr())) + + ctx.Next() + + rw := ctx.Resp.(macaron.ResponseWriter) + l.Log(fmt.Sprintf("[Macaron] Completed %s %s %v %s in %v", ctx.Req.Method, ctx.Req.RequestURI, rw.Status(), http.StatusText(rw.Status()), time.Since(start))) + } +} + // NewMacaron initializes Macaron instance. func NewMacaron() *macaron.Macaron { gob.Register(&u2f.Challenge{}) - m := macaron.New() - if !setting.DisableRouterLog { - m.Use(macaron.Logger()) + var m *macaron.Macaron + if setting.RedirectMacaronLog { + loggerAsWriter := log.NewLoggerAsWriter("INFO") + m = macaron.NewWithLogger(loggerAsWriter) + if !setting.DisableRouterLog { + m.Use(giteaLogger(loggerAsWriter)) + } + } else { + m = macaron.New() + if !setting.DisableRouterLog { + m.Use(macaron.Logger()) + } } m.Use(macaron.Recovery()) if setting.EnableGzip { |