mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 12:10:55 +00:00
Enhanced logging during DEV mode
This commit is contained in:
parent
1fe2226c11
commit
2bd743fcff
@ -18,6 +18,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type logWriter struct {
|
type logWriter struct {
|
||||||
@ -80,3 +81,48 @@ func formatTimeHeader(when time.Time) ([]byte, int) {
|
|||||||
|
|
||||||
return buf[0:], d
|
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 {
|
if BConfig.RunMode == DEV || BConfig.Log.AccessLogs {
|
||||||
timeDur := time.Since(startTime)
|
timeDur := time.Since(startTime)
|
||||||
var devInfo string
|
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 findRouter {
|
||||||
if routerInfo != nil {
|
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 {
|
} 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 {
|
} else {
|
||||||
devInfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s |", r.Method, r.URL.Path, timeDur.String(), "notmatch")
|
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)
|
||||||
if DefaultAccessLogFilter == nil || !DefaultAccessLogFilter.Filter(context) {
|
|
||||||
logs.Debug(devInfo)
|
|
||||||
}
|
}
|
||||||
|
logs.Debug(devInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call WriteHeader if status code has been set changed
|
// Call WriteHeader if status code has been set changed
|
||||||
|
Loading…
Reference in New Issue
Block a user