1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 14:00:54 +00:00

logger color function refactor,easy to read and run more quickly

This commit is contained in:
JessonChan 2019-03-08 11:50:30 +08:00
parent 6dd5171fdf
commit 4a5e108527
2 changed files with 22 additions and 23 deletions

View File

@ -180,7 +180,7 @@ func initColor() {
// 3xx return White // 3xx return White
// 4xx return Yellow // 4xx return Yellow
// 5xx return Red // 5xx return Red
func ColorByStatus(cond bool, code int) string { func ColorByStatus(code int) string {
once.Do(initColor) once.Do(initColor)
switch { switch {
case code >= 200 && code < 300: case code >= 200 && code < 300:
@ -202,7 +202,7 @@ func ColorByStatus(cond bool, code int) string {
// PATCH return Green // PATCH return Green
// HEAD return Magenta // HEAD return Magenta
// OPTIONS return WHITE // OPTIONS return WHITE
func ColorByMethod(cond bool, method string) string { func ColorByMethod(method string) string {
once.Do(initColor) once.Do(initColor)
if c := colorMap[method]; c != "" { if c := colorMap[method]; c != "" {
return c return c
@ -210,6 +210,10 @@ func ColorByMethod(cond bool, method string) string {
return reset return reset
} }
func ResetColor() string {
return reset
}
// Guard Mutex to guarantee atomic of W32Debug(string) function // Guard Mutex to guarantee atomic of W32Debug(string) function
var mu sync.Mutex var mu sync.Mutex

View File

@ -900,34 +900,29 @@ Admin:
} }
if FilterMonitorFunc(r.Method, r.URL.Path, timeDur, pattern, statusCode) { if FilterMonitorFunc(r.Method, r.URL.Path, timeDur, pattern, statusCode) {
routerName := ""
if runRouter != nil { if runRouter != nil {
go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, runRouter.Name(), timeDur) routerName = runRouter.Name()
} else {
go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, "", timeDur)
} }
go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, routerName, timeDur)
} }
} }
if BConfig.RunMode == DEV && !BConfig.Log.AccessLogs { if BConfig.RunMode == DEV && !BConfig.Log.AccessLogs {
var devInfo string match := map[bool]string{true: "match", false: "nomatch"}
iswin := (runtime.GOOS == "windows") devInfo := fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s",
statusColor := logs.ColorByStatus(iswin, statusCode) context.Input.IP(),
methodColor := logs.ColorByMethod(iswin, r.Method) logs.ColorByStatus(statusCode), statusCode, logs.ResetColor(),
resetColor := logs.ColorByMethod(iswin, "") timeDur.String(),
if findRouter { match[findRouter],
logs.ColorByMethod(r.Method), r.Method, logs.ResetColor(),
r.URL.Path)
if routerInfo != nil { if routerInfo != nil {
devInfo = fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s r:%s", context.Input.IP(), statusColor, statusCode, devInfo += fmt.Sprintf(" r:%s", routerInfo.pattern)
resetColor, timeDur.String(), "match", methodColor, r.Method, resetColor, r.URL.Path,
routerInfo.pattern)
} else {
devInfo = fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s", context.Input.IP(), statusColor, statusCode, resetColor,
timeDur.String(), "match", methodColor, r.Method, resetColor, r.URL.Path)
} }
} else {
devInfo = fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s", context.Input.IP(), statusColor, statusCode, resetColor, //todo one logger enough,in fact no need to separate logger into windows and others
timeDur.String(), "nomatch", methodColor, r.Method, resetColor, r.URL.Path) if runtime.GOOS == "windows" {
}
if iswin {
logs.W32Debug(devInfo) logs.W32Debug(devInfo)
} else { } else {
logs.Debug(devInfo) logs.Debug(devInfo)