日志的优雅写法

This commit is contained in:
2023-04-09 05:59:28 +08:00
parent 7910df8f18
commit 3a90035be3

View File

@@ -1,9 +1,11 @@
package main package main
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"runtime" "runtime"
"time"
"regexp" "regexp"
"strconv" "strconv"
@@ -12,9 +14,9 @@ import (
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
func init() { //func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) // log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
} //}
// string 转换为 int, 如果转换失败则返回默认值 // string 转换为 int, 如果转换失败则返回默认值
func stringToInt(str string, defaultValue int) int { func stringToInt(str string, defaultValue int) int {
@@ -28,6 +30,11 @@ func stringToInt(str string, defaultValue int) int {
return value return value
} }
func LogComponent(startTime int64, r *http.Request) {
endTime := fmt.Sprintf("%dms", (time.Now().UnixNano()-startTime)/1000000)
log.Println(r.Method, r.URL.Path, endTime)
}
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
@@ -36,19 +43,19 @@ func main() {
// URL 格式: /img/{type}-{id}.{format}?width=320&height=320&fit=cover // URL 格式: /img/{type}-{id}.{format}?width=320&height=320&fit=cover
http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL.Path) defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
reg := regexp.MustCompile(`^/img/([0-9a-zA-Z]+)-([0-9a-zA-Z]+).(jpg|jpeg|png|webp)$`) reg := regexp.MustCompile(`^/img/([0-9a-zA-Z]+)-([0-9a-zA-Z]+).(jpg|jpeg|png|webp)$`)
matches := reg.FindStringSubmatch(r.URL.Path) matches := reg.FindStringSubmatch(r.URL.Path)
if len(matches) != 4 { if len(matches) != 4 {
log.Println("URL 格式错误", matches) http.Error(w, "URL 格式错误", http.StatusNotFound)
w.WriteHeader(http.StatusNotFound)
return return
} }
group, id, format, width, height, fit := matches[1], matches[2], matches[3], stringToInt(r.URL.Query().Get("width"), 0), stringToInt(r.URL.Query().Get("height"), 0), r.URL.Query().Get("fit") group, id, format, width, height, fit := matches[1], matches[2], matches[3], stringToInt(r.URL.Query().Get("width"), 0), stringToInt(r.URL.Query().Get("height"), 0), r.URL.Query().Get("fit")
content, err := mysqlConnection.GetImageContent(group, id) content, err := mysqlConnection.GetImageContent(group, id)
if err != nil { if err != nil {
log.Println("获取图片失败", format, err) log.Println("获取图片失败", format, err)
w.WriteHeader(http.StatusNotFound) http.Error(w, err.Error(), http.StatusNotFound)
return return
} }
var img models.Image var img models.Image
@@ -56,7 +63,7 @@ func main() {
data, err := img.ToWebP(width, height, fit) data, err := img.ToWebP(width, height, fit)
if err != nil { if err != nil {
log.Println("转换图片失败", err) log.Println("转换图片失败", err)
w.WriteHeader(http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
@@ -67,7 +74,8 @@ func main() {
// URL 格式: /webp/{type}-{id}-{version}-{width}-{height}-{fit}.{format} // URL 格式: /webp/{type}-{id}-{version}-{width}-{height}-{fit}.{format}
http.HandleFunc("/webp/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/webp/", func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL.Path) defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
reg := regexp.MustCompile(`^/webp/([0-9a-zA-Z]+)-([0-9a-zA-Z]+)-([0-9a-zA-Z]+)-([0-9]+)-([0-9]+)-([a-zA-Z]+).(jpg|jpeg|png|webp)$`) reg := regexp.MustCompile(`^/webp/([0-9a-zA-Z]+)-([0-9a-zA-Z]+)-([0-9a-zA-Z]+)-([0-9]+)-([0-9]+)-([a-zA-Z]+).(jpg|jpeg|png|webp)$`)
matches := reg.FindStringSubmatch(r.URL.Path) matches := reg.FindStringSubmatch(r.URL.Path)
if len(matches) != 8 { if len(matches) != 8 {