Handle logger levels

This commit is contained in:
Dan Sosedoff 2022-12-01 12:49:24 -06:00
parent c996d71378
commit 72ecd20dd1
No known key found for this signature in database
GPG Key ID: 26186197D282B164
2 changed files with 14 additions and 3 deletions

View File

@ -12,6 +12,8 @@ import (
const loggerMessage = "http_request"
func RequestLogger(logger *logrus.Logger) gin.HandlerFunc {
debug := logger.Level > logrus.InfoLevel
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
@ -20,7 +22,7 @@ func RequestLogger(logger *logrus.Logger) gin.HandlerFunc {
c.Next()
// Skip logging static assets
if strings.Contains(path, "/static/") {
if strings.Contains(path, "/static/") && !debug {
return
}
@ -40,6 +42,11 @@ func RequestLogger(logger *logrus.Logger) gin.HandlerFunc {
fields["error"] = err.Error()
}
// Additional fields for debugging
if debug {
fields["raw_query"] = c.Request.URL.RawQuery
}
entry := logrus.WithFields(fields)
switch {

View File

@ -184,9 +184,13 @@ func printVersion() {
}
func startServer() {
router := gin.New()
logger := logrus.New()
if options.Debug {
logger.SetLevel(logrus.DebugLevel)
}
router.Use(api.RequestLogger(logrus.New()))
router := gin.New()
router.Use(api.RequestLogger(logger))
router.Use(gin.Recovery())
// Enable HTTP basic authentication only if both user and password are set