From 495ad4defd9b1b1f4094031a9d1956e6a2cd253b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 13 Jun 2020 10:54:38 -0700 Subject: [PATCH] Cleaner gin logging in debug --- cmd/root.go | 3 --- router/router.go | 21 ++++++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 918baae..71c7656 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,7 +4,6 @@ import ( "crypto/tls" "fmt" "github.com/apex/log" - "github.com/gin-gonic/gin" "github.com/mitchellh/colorstring" "github.com/pterodactyl/wings/loggers/cli" "net/http" @@ -224,8 +223,6 @@ func rootCmdRun(*cobra.Command, []string) { "port": c.Api.Port, }).Info("configuring webserver...") - gin.SetMode("release") - r := router.Configure() addr := fmt.Sprintf("%s:%d", c.Api.Host, c.Api.Port) diff --git a/router/router.go b/router/router.go index 99d9d1f..162dc57 100644 --- a/router/router.go +++ b/router/router.go @@ -1,13 +1,32 @@ package router import ( + "github.com/apex/log" "github.com/gin-gonic/gin" ) // Configures the routing infrastructure for this daemon instance. func Configure() *gin.Engine { - router := gin.Default() + gin.SetMode("release") + + router := gin.New() + + router.Use(gin.Recovery()) router.Use(SetAccessControlHeaders) + // @todo log this into a different file so you can setup IP blocking for abusive requests and such. + // This should still dump requests in debug mode since it does help with understanding the request + // lifecycle and quickly seeing what was called leading to the logs. However, it isn't feasible to mix + // this output in production and still get meaningful logs from it since they'll likely just be a huge + // spamfest. + router.Use(gin.LoggerWithFormatter(func(params gin.LogFormatterParams) string { + log.WithFields(log.Fields{ + "client_ip": params.ClientIP, + "status": params.StatusCode, + "latency": params.Latency, + }).Debugf("%s %s", params.MethodColor()+params.Method+params.ResetColor(), params.Path) + + return "" + })) router.OPTIONS("/api/system", func(c *gin.Context) { c.Status(200)