aboutsummaryrefslogtreecommitdiff
path: root/modules/middleware/logger.go
diff options
context:
space:
mode:
authorFuXiaoHei <fuxiaohei@hexiaz.com>2014-03-19 20:34:11 +0800
committerFuXiaoHei <fuxiaohei@hexiaz.com>2014-03-19 20:34:11 +0800
commit78097a2c5a79ec93340db975c23014de2a329572 (patch)
tree1d9bc1f5422d03eca18269fc1c6b90ebe8910d20 /modules/middleware/logger.go
parent9533b23272097c538e25692ec91c37f4ea6c0b81 (diff)
parentde087c7b4a31cb0643d5432ec9d6b26e208baff2 (diff)
Merge branch 'master' of https://github.com/gogits/gogs
Diffstat (limited to 'modules/middleware/logger.go')
-rw-r--r--modules/middleware/logger.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/modules/middleware/logger.go b/modules/middleware/logger.go
new file mode 100644
index 00000000..dcf85246
--- /dev/null
+++ b/modules/middleware/logger.go
@@ -0,0 +1,46 @@
+// 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;32m%s\033[0m", content)
+ case 304:
+ content = fmt.Sprintf("\033[1;33m%s\033[0m", content)
+ case 404:
+ content = fmt.Sprintf("\033[1;31m%s\033[0m", content)
+ case 500:
+ content = fmt.Sprintf("\033[1;36m%s\033[0m", content)
+ }
+ }
+ log.Println(content)
+ }
+}