Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-12-01 06:16:09 +01:00
forgejo/modules/middleware/logger.go

53 lines
1.1 KiB
Go
Raw Normal View History

2014-03-19 10:31:38 +01:00
// 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"
2014-03-30 18:11:28 +02:00
"github.com/go-martini/martini"
2014-05-02 00:53:41 +02:00
"github.com/gogits/gogs/modules/base"
2014-03-19 10:31:38 +01:00
)
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) {
2014-05-06 19:47:47 +02:00
if base.DisableRouterLog {
2014-05-02 00:53:41 +02:00
return
}
2014-03-19 10:31:38 +01:00
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:
2014-03-19 10:35:52 +01:00
content = fmt.Sprintf("\033[1;32m%s\033[0m", content)
2014-03-19 10:31:38 +01:00
case 304:
2014-03-19 10:35:52 +01:00
content = fmt.Sprintf("\033[1;33m%s\033[0m", content)
2014-03-19 10:31:38 +01:00
case 404:
2014-03-19 10:35:52 +01:00
content = fmt.Sprintf("\033[1;31m%s\033[0m", content)
2014-03-19 12:21:23 +01:00
case 500:
content = fmt.Sprintf("\033[1;36m%s\033[0m", content)
2014-03-19 10:31:38 +01:00
}
}
log.Println(content)
}
}