mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 23:00:50 +00:00
Enhanced logging during DEV mode
This commit is contained in:
parent
1fe2226c11
commit
2bd743fcff
@ -18,6 +18,7 @@ import (
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type logWriter struct {
|
||||
@ -80,3 +81,48 @@ func formatTimeHeader(when time.Time) ([]byte, int) {
|
||||
|
||||
return buf[0:], d
|
||||
}
|
||||
|
||||
var (
|
||||
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
|
||||
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
|
||||
yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
|
||||
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
|
||||
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
|
||||
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
|
||||
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
|
||||
reset = string([]byte{27, 91, 48, 109})
|
||||
)
|
||||
|
||||
func ColorByStatus(code int) string {
|
||||
switch {
|
||||
case code >= 200 && code < 300:
|
||||
return green
|
||||
case code >= 300 && code < 400:
|
||||
return white
|
||||
case code >= 400 && code < 500:
|
||||
return yellow
|
||||
default:
|
||||
return red
|
||||
}
|
||||
}
|
||||
|
||||
func ColorByMethod(method string) string {
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
return blue
|
||||
case http.MethodPost:
|
||||
return cyan
|
||||
case http.MethodPut:
|
||||
return yellow
|
||||
case http.MethodDelete:
|
||||
return red
|
||||
case http.MethodPatch:
|
||||
return green
|
||||
case http.MethodHead:
|
||||
return magenta
|
||||
case http.MethodOptions:
|
||||
return white
|
||||
default:
|
||||
return reset
|
||||
}
|
||||
}
|
||||
|
21
router.go
21
router.go
@ -816,18 +816,27 @@ Admin:
|
||||
if BConfig.RunMode == DEV || BConfig.Log.AccessLogs {
|
||||
timeDur := time.Since(startTime)
|
||||
var devInfo string
|
||||
|
||||
statusCode := context.ResponseWriter.Status
|
||||
if statusCode == 0 { statusCode = 200 }
|
||||
|
||||
statusColor := logs.ColorByStatus(statusCode)
|
||||
methodColor := logs.ColorByMethod(r.Method)
|
||||
resetColor := logs.ColorByMethod("")
|
||||
|
||||
if findRouter {
|
||||
if routerInfo != nil {
|
||||
devInfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s | % -40s |", r.Method, r.URL.Path, timeDur.String(), "match", routerInfo.pattern)
|
||||
devInfo = fmt.Sprintf("|%s %3d %s|%7s|%8s|%s %s %-7s %-3s r:%s", statusColor, statusCode, resetColor,
|
||||
timeDur.String(), "match", methodColor, resetColor, r.Method, r.URL.Path, routerInfo.pattern)
|
||||
} else {
|
||||
devInfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s |", r.Method, r.URL.Path, timeDur.String(), "match")
|
||||
devInfo = fmt.Sprintf("|%s %3d %s|%7s|%8s|%s %s %-7s %-3s", statusColor, statusCode, resetColor,
|
||||
timeDur.String(), "match", methodColor, resetColor, r.Method, r.URL.Path)
|
||||
}
|
||||
} else {
|
||||
devInfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s |", r.Method, r.URL.Path, timeDur.String(), "notmatch")
|
||||
}
|
||||
if DefaultAccessLogFilter == nil || !DefaultAccessLogFilter.Filter(context) {
|
||||
logs.Debug(devInfo)
|
||||
devInfo = fmt.Sprintf("|%s %3d %s|%7s|%8s|%s %s %-7s %-3s", statusColor, statusCode, resetColor,
|
||||
timeDur.String(), "nomatch", methodColor, resetColor, r.Method, r.URL.Path)
|
||||
}
|
||||
logs.Debug(devInfo)
|
||||
}
|
||||
|
||||
// Call WriteHeader if status code has been set changed
|
||||
|
Loading…
Reference in New Issue
Block a user