mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-27 12:53:57 +01:00
Use custom logger middleware
This commit is contained in:
parent
c593578a55
commit
04e5d5b71f
2 changed files with 56 additions and 1 deletions
44
modules/middleware/logger.go
Normal file
44
modules/middleware/logger.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/codegangsta/martini"
|
||||
)
|
||||
|
||||
var isWindows bool
|
||||
|
||||
func init() {
|
||||
isWindows = runtime.GOOS == "windows"
|
||||
}
|
||||
|
||||
func Logger() martini.Handler {
|
||||
return func(res http.ResponseWriter, req *http.Request, ctx martini.Context, log *log.Logger) {
|
||||
start := time.Now()
|
||||
log.Printf("Started %s %s", req.Method, req.URL.Path)
|
||||
|
||||
rw := res.(martini.ResponseWriter)
|
||||
ctx.Next()
|
||||
|
||||
content := fmt.Sprintf("Completed %v %s in %v", rw.Status(), http.StatusText(rw.Status()), time.Since(start))
|
||||
if !isWindows {
|
||||
switch rw.Status() {
|
||||
case 200:
|
||||
content = fmt.Sprintf("\033[1;32%s\033[0m", content)
|
||||
case 304:
|
||||
content = fmt.Sprintf("\033[1;33%s\033[0m", content)
|
||||
case 404:
|
||||
content = fmt.Sprintf("\033[1;31%s\033[0m", content)
|
||||
}
|
||||
}
|
||||
log.Println(content)
|
||||
}
|
||||
}
|
13
web.go
13
web.go
|
@ -46,11 +46,22 @@ func checkRunMode() {
|
|||
log.Info("Run Mode: %s", strings.Title(martini.Env))
|
||||
}
|
||||
|
||||
func newMartini() *martini.ClassicMartini {
|
||||
r := martini.NewRouter()
|
||||
m := martini.New()
|
||||
m.Use(middleware.Logger())
|
||||
m.Use(martini.Recovery())
|
||||
m.Use(martini.Static("public"))
|
||||
m.MapTo(r, (*martini.Routes)(nil))
|
||||
m.Action(r.Handle)
|
||||
return &martini.ClassicMartini{m, r}
|
||||
}
|
||||
|
||||
func runWeb(*cli.Context) {
|
||||
checkRunMode()
|
||||
log.Info("%s %s", base.AppName, base.AppVer)
|
||||
|
||||
m := martini.Classic()
|
||||
m := newMartini()
|
||||
|
||||
// Middlewares.
|
||||
m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{base.TemplateFuncs}}))
|
||||
|
|
Loading…
Reference in a new issue