@@ 7,6 7,7 @@
package main
import (
+ "fmt"
"log"
"net/http"
"strconv"
@@ 25,8 26,35 @@ var (
g errgroup.Group
)
+func ginLogFormat(param gin.LogFormatterParams, router string) string {
+ var statusColor, methodColor, resetColor string
+ if param.IsOutputColor() {
+ statusColor = param.StatusCodeColor()
+ methodColor = param.MethodColor()
+ resetColor = param.ResetColor()
+ }
+
+ if param.Latency > time.Minute {
+ param.Latency = param.Latency.Truncate(time.Second)
+ }
+ return fmt.Sprintf("[GIN] [felu/%-8s] | %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
+ router,
+ param.TimeStamp.Format("2006/01/02 - 15:04:05"),
+ statusColor, param.StatusCode, resetColor,
+ param.Latency,
+ param.ClientIP,
+ methodColor, param.Method, resetColor,
+ param.Path,
+ param.ErrorMessage,
+ )
+}
+
func setupFrontendRouter() *gin.Engine {
- r := gin.Default()
+ r := gin.New()
+ r.Use(gin.Recovery())
+ r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
+ return ginLogFormat(param, "frontend")
+ }))
r.Static("/static", "./static")
r.HTMLRender = &TemplRender{}
@@ 38,7 66,11 @@ func setupFrontendRouter() *gin.Engine {
}
func setupBackendRouter() *gin.Engine {
- r := gin.Default()
+ r := gin.New()
+ r.Use(gin.Recovery())
+ r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
+ return ginLogFormat(param, "backend")
+ }))
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{