diff --git a/src/bkauth/pkg/database/utils.go b/src/bkauth/pkg/database/utils.go index 797284e..2b90c5d 100644 --- a/src/bkauth/pkg/database/utils.go +++ b/src/bkauth/pkg/database/utils.go @@ -60,7 +60,7 @@ func logSlowSQL(start time.Time, query string, args interface{}) { // current, set 20ms if latency > 20 { - logger.Debug( + logger.Error( "-", zap.String("sql", strings.ReplaceAll(query, "\n\t\t", " ")), zap.String("sql", truncateArgs(args, ArgsTruncateLength)), diff --git a/src/bkauth/pkg/middleware/request_id.go b/src/bkauth/pkg/middleware/request_id.go index ceaaee1..7525701 100644 --- a/src/bkauth/pkg/middleware/request_id.go +++ b/src/bkauth/pkg/middleware/request_id.go @@ -34,7 +34,7 @@ func RequestID() gin.HandlerFunc { zap.S().Debug("Middleware: RequestID") requestID := c.GetHeader(util.RequestIDHeaderKey) - if requestID == "" || len(requestID) != 32 { + if requestID == "" { requestID = hex.EncodeToString(uuid.Must(uuid.NewV4()).Bytes()) } util.SetRequestID(c, requestID) diff --git a/src/bkauth/pkg/server/router.go b/src/bkauth/pkg/server/router.go index 726ef1a..5e65c1f 100644 --- a/src/bkauth/pkg/server/router.go +++ b/src/bkauth/pkg/server/router.go @@ -19,6 +19,9 @@ package server import ( + "fmt" + "time" + "github.com/gin-gonic/gin" "bkauth/pkg/api/app" @@ -26,6 +29,7 @@ import ( "bkauth/pkg/api/oauth" "bkauth/pkg/config" "bkauth/pkg/middleware" + "bkauth/pkg/util" ) // NewRouter ... @@ -39,7 +43,7 @@ func NewRouter(cfg *config.Config) *gin.Engine { // router := gin.Default() router := gin.New() // MW: gin default logger - router.Use(gin.Logger()) + router.Use(gin.LoggerWithFormatter(ginLogFormat)) // MW: recovery with sentry router.Use(middleware.Recovery(cfg.Sentry.Enable)) // MW: request_id @@ -65,3 +69,18 @@ func NewRouter(cfg *config.Config) *gin.Engine { return router } +func ginLogFormat(param gin.LogFormatterParams) string { + // your custom format + return fmt.Sprintf("%s - [%s] \"%s %s %s %s %d \"%s\" %s %s\"\n", + param.ClientIP, + param.TimeStamp.Format(time.RFC1123), + param.Method, + param.Path, + param.Request.Proto, + param.Request.Header.Get(util.RequestIDHeaderKey), + param.StatusCode, + param.Latency, + param.Request.UserAgent(), + param.ErrorMessage, + ) +}