mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 04:35:40 +00:00
feat: better health check and less log noise (#274)
* feat: better health check and less log noise * feat: better health check and less log noise
This commit is contained in:
@@ -21,6 +21,23 @@ type Server struct {
|
|||||||
Router *gin.Engine
|
Router *gin.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
loggerSkipPathsPrefix = []string{
|
||||||
|
"GET /api/healthcheck",
|
||||||
|
"HEAD /api/healthcheck",
|
||||||
|
"GET /favicon.ico",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func logPath(path string) bool{
|
||||||
|
for _, prefix := range loggerSkipPathsPrefix {
|
||||||
|
if strings.HasPrefix(path, prefix) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server, error) {
|
func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server, error) {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
|
||||||
@@ -68,6 +85,7 @@ func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server,
|
|||||||
|
|
||||||
// App routes
|
// App routes
|
||||||
router.GET("/api/healthcheck", handlers.HealthcheckHandler)
|
router.GET("/api/healthcheck", handlers.HealthcheckHandler)
|
||||||
|
router.HEAD("/api/healthcheck", handlers.HealthcheckHandler)
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
Config: config,
|
Config: config,
|
||||||
@@ -100,13 +118,17 @@ func zerolog() gin.HandlerFunc {
|
|||||||
latency := time.Since(tStart).String()
|
latency := time.Since(tStart).String()
|
||||||
|
|
||||||
// Log request
|
// Log request
|
||||||
switch {
|
if logPath(method + " " + path) {
|
||||||
case code >= 200 && code < 300:
|
switch {
|
||||||
log.Info().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
|
case code >= 200 && code < 300:
|
||||||
case code >= 300 && code < 400:
|
log.Info().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
|
||||||
log.Warn().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
|
case code >= 300 && code < 400:
|
||||||
case code >= 400:
|
log.Warn().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
|
||||||
log.Error().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
|
case code >= 400:
|
||||||
}
|
log.Error().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
log.Debug().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user