diff options
author | Unknown <joe2010xtmf@163.com> | 2014-03-19 05:31:38 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-03-19 05:31:38 -0400 |
commit | 04e5d5b71fe39039baf72b27411e7629edbcc6a2 (patch) | |
tree | bcb18b59cf8eed9ff139f3d7da2f3a696a386f8d /modules | |
parent | c593578a55d2f341fe1690d85ea1b1ec45bea50a (diff) | |
download | gitea-04e5d5b71fe39039baf72b27411e7629edbcc6a2.tar.gz gitea-04e5d5b71fe39039baf72b27411e7629edbcc6a2.zip |
Use custom logger middleware
Diffstat (limited to 'modules')
-rw-r--r-- | modules/middleware/logger.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/modules/middleware/logger.go b/modules/middleware/logger.go new file mode 100644 index 0000000000..9bbedb29a3 --- /dev/null +++ b/modules/middleware/logger.go @@ -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) + } +} |