mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 03:11:30 +00:00
added support for Windows terminals
This commit is contained in:
parent
844a3b0ffd
commit
479dfdbd40
@ -18,6 +18,9 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"github.com/shiena/ansicolor"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type logWriter struct {
|
type logWriter struct {
|
||||||
@ -59,14 +62,14 @@ func formatTimeHeader(when time.Time) ([]byte, int) {
|
|||||||
buf[0] = '2'
|
buf[0] = '2'
|
||||||
//change to '1' after 84 years, LOL
|
//change to '1' after 84 years, LOL
|
||||||
buf[1] = '0'
|
buf[1] = '0'
|
||||||
buf[2] = y1[y-2000]
|
buf[2] = y1[y - 2000]
|
||||||
buf[3] = y2[y-2000]
|
buf[3] = y2[y - 2000]
|
||||||
buf[4] = '/'
|
buf[4] = '/'
|
||||||
buf[5] = mo1[mo-1]
|
buf[5] = mo1[mo - 1]
|
||||||
buf[6] = mo2[mo-1]
|
buf[6] = mo2[mo - 1]
|
||||||
buf[7] = '/'
|
buf[7] = '/'
|
||||||
buf[8] = d1[d-1]
|
buf[8] = d1[d - 1]
|
||||||
buf[9] = d2[d-1]
|
buf[9] = d2[d - 1]
|
||||||
buf[10] = ' '
|
buf[10] = ' '
|
||||||
buf[11] = h1[h]
|
buf[11] = h1[h]
|
||||||
buf[12] = h2[h]
|
buf[12] = h2[h]
|
||||||
@ -82,46 +85,69 @@ func formatTimeHeader(when time.Time) ([]byte, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
|
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
|
||||||
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
|
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
|
||||||
yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
|
yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
|
||||||
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
|
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
|
||||||
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
|
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
|
||||||
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
|
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
|
||||||
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
|
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
|
||||||
reset = string([]byte{27, 91, 48, 109})
|
|
||||||
|
w32Green = string([]byte{27, 91, 52, 50, 109})
|
||||||
|
w32White = string([]byte{27, 91, 52, 55, 109})
|
||||||
|
w32Yellow = string([]byte{27, 91, 52, 51, 109})
|
||||||
|
w32Red = string([]byte{27, 91, 52, 49, 109})
|
||||||
|
w32Blue = string([]byte{27, 91, 52, 52, 109})
|
||||||
|
w32Magenta = string([]byte{27, 91, 52, 53, 109})
|
||||||
|
w32Cyan = string([]byte{27, 91, 52, 54, 109})
|
||||||
|
|
||||||
|
reset = string([]byte{27, 91, 48, 109})
|
||||||
)
|
)
|
||||||
|
|
||||||
func ColorByStatus(code int) string {
|
func ColorByStatus(cond bool, code int) string {
|
||||||
switch {
|
switch {
|
||||||
case code >= 200 && code < 300:
|
case code >= 200 && code < 300:
|
||||||
return green
|
return map[bool]string{true: green, false: w32Green}[cond]
|
||||||
case code >= 300 && code < 400:
|
case code >= 300 && code < 400:
|
||||||
return white
|
return map[bool]string{true: white, false: w32White}[cond]
|
||||||
case code >= 400 && code < 500:
|
case code >= 400 && code < 500:
|
||||||
return yellow
|
return map[bool]string{true: yellow, false: w32Yellow}[cond]
|
||||||
default:
|
default:
|
||||||
return red
|
return map[bool]string{true: red, false: w32Red}[cond]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ColorByMethod(method string) string {
|
func ColorByMethod(cond bool, method string) string {
|
||||||
switch method {
|
switch method {
|
||||||
case "GET":
|
case "GET":
|
||||||
return blue
|
return map[bool]string{true: blue, false: w32Blue}[cond]
|
||||||
case "POST":
|
case "POST":
|
||||||
return cyan
|
return map[bool]string{true: cyan, false: w32Cyan}[cond]
|
||||||
case "PUT":
|
case "PUT":
|
||||||
return yellow
|
return map[bool]string{true: yellow, false: w32Yellow}[cond]
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
return red
|
return map[bool]string{true: red, false: w32Red}[cond]
|
||||||
case "PATCH":
|
case "PATCH":
|
||||||
return green
|
return map[bool]string{true: green, false: w32Green}[cond]
|
||||||
case "HEAD":
|
case "HEAD":
|
||||||
return magenta
|
return map[bool]string{true: magenta, false: w32Magenta}[cond]
|
||||||
case "OPTIONS":
|
case "OPTIONS":
|
||||||
return white
|
return map[bool]string{true: white, false: w32White}[cond]
|
||||||
default:
|
default:
|
||||||
return reset
|
return reset
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
// Helper method to output colored logs in Windows terminals
|
||||||
|
// using ansicolor (https://github.com/shiena/ansicolor)
|
||||||
|
func W32Debug(msg string) {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
current := time.Now()
|
||||||
|
w := ansicolor.NewAnsiColorWriter(os.Stdout)
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "[beego] %v %s\n", current.Format("2006/01/02 - 15:04:05"), msg)
|
||||||
|
}
|
||||||
|
22
router.go
22
router.go
@ -820,23 +820,29 @@ Admin:
|
|||||||
statusCode := context.ResponseWriter.Status
|
statusCode := context.ResponseWriter.Status
|
||||||
if statusCode == 0 { statusCode = 200 }
|
if statusCode == 0 { statusCode = 200 }
|
||||||
|
|
||||||
statusColor := logs.ColorByStatus(statusCode)
|
iswin := (runtime.GOOS == "windows")
|
||||||
methodColor := logs.ColorByMethod(r.Method)
|
statusColor := logs.ColorByStatus(iswin, statusCode)
|
||||||
resetColor := logs.ColorByMethod("")
|
methodColor := logs.ColorByMethod(iswin, r.Method)
|
||||||
|
resetColor := logs.ColorByMethod(iswin, "")
|
||||||
|
|
||||||
if findRouter {
|
if findRouter {
|
||||||
if routerInfo != nil {
|
if routerInfo != nil {
|
||||||
devInfo = fmt.Sprintf("|%s %3d %s|%7s|%8s|%s %s %-7s %-3s r:%s", statusColor, statusCode, resetColor,
|
devInfo = fmt.Sprintf("|%s %3d %s|%13s|%8s|%s %s %-7s %-3s r:%s", statusColor, statusCode,
|
||||||
timeDur.String(), "match", methodColor, resetColor, r.Method, r.URL.Path, routerInfo.pattern)
|
resetColor, timeDur.String(), "match", methodColor, resetColor, r.Method, r.URL.Path,
|
||||||
|
routerInfo.pattern)
|
||||||
} else {
|
} else {
|
||||||
devInfo = fmt.Sprintf("|%s %3d %s|%7s|%8s|%s %s %-7s %-3s", statusColor, statusCode, resetColor,
|
devInfo = fmt.Sprintf("|%s %3d %s|%13s|%8s|%s %s %-7s %-3s", statusColor, statusCode, resetColor,
|
||||||
timeDur.String(), "match", methodColor, resetColor, r.Method, r.URL.Path)
|
timeDur.String(), "match", methodColor, resetColor, r.Method, r.URL.Path)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
devInfo = fmt.Sprintf("|%s %3d %s|%7s|%8s|%s %s %-7s %-3s", statusColor, statusCode, resetColor,
|
devInfo = fmt.Sprintf("|%s %3d %s|%13s|%8s|%s %s %-7s %-3s", statusColor, statusCode, resetColor,
|
||||||
timeDur.String(), "nomatch", methodColor, resetColor, r.Method, r.URL.Path)
|
timeDur.String(), "nomatch", methodColor, resetColor, r.Method, r.URL.Path)
|
||||||
}
|
}
|
||||||
logs.Debug(devInfo)
|
if iswin {
|
||||||
|
logs.W32Debug(devInfo)
|
||||||
|
} else {
|
||||||
|
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