彩色控制臺

This commit is contained in:
2023-04-13 20:46:25 +08:00
parent 54f4baa1e1
commit 752e0b98e0
3 changed files with 27 additions and 2 deletions

View File

@@ -33,8 +33,25 @@ func stringToInt(str string, defaultValue int) int {
}
func LogComponent(startTime int64, r *http.Request) {
endTime := fmt.Sprintf("%dms", (time.Now().UnixNano()-startTime)/1000000)
log.Println(r.Method, r.URL, endTime)
ms := (time.Now().UnixNano() - startTime) / 1000000
color := "\033[1;32m%d\033[0m"
if ms > 800 {
color = "\033[1;31m%dms\033[0m" // 紅色加重
} else if ms > 500 {
color = "\033[1;33m%dms\033[0m" // 黃色加重
} else if ms > 300 {
color = "\033[1;32m%dms\033[0m" // 綠色加重
} else if ms > 200 {
color = "\033[1;34m%dms\033[0m" // 藍色加重
} else if ms > 100 {
color = "\033[1;35m%dms\033[0m" // 紫色加重
} else {
color = "\033[1;36m%dms\033[0m" // 黑色加重
}
endTime := fmt.Sprintf(color, ms)
method := fmt.Sprintf("\033[1;32m%s\033[0m", r.Method) // 綠色加重
url := fmt.Sprintf("\033[1;34m%s\033[0m", r.URL) // 藍色加重
log.Println(method, url, endTime)
}
type User struct {
@@ -174,6 +191,11 @@ func main() {
return
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
w.Write([]byte("Hello World!"))
})
// 获取浏览记录
http.HandleFunc("/history", func(w http.ResponseWriter, r *http.Request) {
defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志