mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 22:11:29 +00:00
logger color function refactor,easy to read and run more quickly
This commit is contained in:
parent
1f93040af6
commit
6dd5171fdf
@ -18,6 +18,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -146,21 +147,50 @@ var (
|
|||||||
reset = string([]byte{27, 91, 48, 109})
|
reset = string([]byte{27, 91, 48, 109})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var once sync.Once
|
||||||
|
var colorMap map[string]string
|
||||||
|
|
||||||
|
func initColor() {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
green = w32Green
|
||||||
|
white = w32White
|
||||||
|
yellow = w32Yellow
|
||||||
|
red = w32Red
|
||||||
|
blue = w32Blue
|
||||||
|
magenta = w32Magenta
|
||||||
|
cyan = w32Cyan
|
||||||
|
}
|
||||||
|
colorMap = map[string]string{
|
||||||
|
"green": green,
|
||||||
|
"white": white,
|
||||||
|
"yellow": yellow,
|
||||||
|
"red": red,
|
||||||
|
"GET": blue,
|
||||||
|
"POST": cyan,
|
||||||
|
"PUT": yellow,
|
||||||
|
"DELETE": red,
|
||||||
|
"PATCH": green,
|
||||||
|
"HEAD": magenta,
|
||||||
|
"OPTIONS": white,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ColorByStatus return color by http code
|
// ColorByStatus return color by http code
|
||||||
// 2xx return Green
|
// 2xx return Green
|
||||||
// 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(cond bool, code int) string {
|
||||||
|
once.Do(initColor)
|
||||||
switch {
|
switch {
|
||||||
case code >= 200 && code < 300:
|
case code >= 200 && code < 300:
|
||||||
return map[bool]string{true: green, false: w32Green}[cond]
|
return colorMap["green"]
|
||||||
case code >= 300 && code < 400:
|
case code >= 300 && code < 400:
|
||||||
return map[bool]string{true: white, false: w32White}[cond]
|
return colorMap["white"]
|
||||||
case code >= 400 && code < 500:
|
case code >= 400 && code < 500:
|
||||||
return map[bool]string{true: yellow, false: w32Yellow}[cond]
|
return colorMap["yellow"]
|
||||||
default:
|
default:
|
||||||
return map[bool]string{true: red, false: w32Red}[cond]
|
return colorMap["red"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,24 +203,11 @@ func ColorByStatus(cond bool, code int) string {
|
|||||||
// HEAD return Magenta
|
// HEAD return Magenta
|
||||||
// OPTIONS return WHITE
|
// OPTIONS return WHITE
|
||||||
func ColorByMethod(cond bool, method string) string {
|
func ColorByMethod(cond bool, method string) string {
|
||||||
switch method {
|
once.Do(initColor)
|
||||||
case "GET":
|
if c := colorMap[method]; c != "" {
|
||||||
return map[bool]string{true: blue, false: w32Blue}[cond]
|
return c
|
||||||
case "POST":
|
|
||||||
return map[bool]string{true: cyan, false: w32Cyan}[cond]
|
|
||||||
case "PUT":
|
|
||||||
return map[bool]string{true: yellow, false: w32Yellow}[cond]
|
|
||||||
case "DELETE":
|
|
||||||
return map[bool]string{true: red, false: w32Red}[cond]
|
|
||||||
case "PATCH":
|
|
||||||
return map[bool]string{true: green, false: w32Green}[cond]
|
|
||||||
case "HEAD":
|
|
||||||
return map[bool]string{true: magenta, false: w32Magenta}[cond]
|
|
||||||
case "OPTIONS":
|
|
||||||
return map[bool]string{true: white, false: w32White}[cond]
|
|
||||||
default:
|
|
||||||
return reset
|
|
||||||
}
|
}
|
||||||
|
return reset
|
||||||
}
|
}
|
||||||
|
|
||||||
// Guard Mutex to guarantee atomic of W32Debug(string) function
|
// Guard Mutex to guarantee atomic of W32Debug(string) function
|
||||||
|
Loading…
Reference in New Issue
Block a user