mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-19 04:06:18 +08:00

* feat(log):Add configurable log filtering middleware for HTTP requests Implement a comprehensive log filtering system that allows selective suppression of HTTP request logs based on paths, methods, and prefixes. The system includes environment variable configuration support and filters health checks, WebDAV requests, and HEAD requests by default to reduce log noise. * fix(log):Replace gin.DefaultLogFormatter with custom implementation * Remove filtered logger test file * fix(log):Refactor log filtering to use centralized configuration and add server-specific filtering * fix(log):Add documentation comments for log filtering configuration
55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
package middlewares
|
|
|
|
import (
|
|
"github.com/OpenListTeam/OpenList/v4/internal/conf"
|
|
"github.com/gin-gonic/gin"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// UnifiedFilteredLogger returns a filtered logger using global configuration
|
|
// serverType: "http" for main HTTP server, "s3" for S3 server
|
|
func UnifiedFilteredLogger(serverType string) gin.HandlerFunc {
|
|
config := conf.Conf.Log.Filter
|
|
|
|
if !config.EnableFiltering {
|
|
// Return standard Gin logger if filtering is disabled
|
|
return gin.LoggerWithWriter(log.StandardLogger().Out)
|
|
}
|
|
|
|
loggerConfig := FilteredLoggerConfig{
|
|
Output: log.StandardLogger().Out,
|
|
}
|
|
|
|
// Add health check paths
|
|
if config.FilterHealthChecks {
|
|
loggerConfig.SkipPaths = append(loggerConfig.SkipPaths, "/ping")
|
|
}
|
|
|
|
// Add HEAD method filtering
|
|
if config.FilterHEADRequests {
|
|
loggerConfig.SkipMethods = append(loggerConfig.SkipMethods, "HEAD")
|
|
}
|
|
|
|
// Add WebDAV filtering only for HTTP server (not for S3)
|
|
if config.FilterWebDAV && serverType == "http" {
|
|
loggerConfig.SkipPathPrefixes = append(loggerConfig.SkipPathPrefixes, "/dav/")
|
|
loggerConfig.SkipMethods = append(loggerConfig.SkipMethods, "PROPFIND")
|
|
}
|
|
|
|
// Add custom configurations
|
|
loggerConfig.SkipPaths = append(loggerConfig.SkipPaths, config.CustomSkipPaths...)
|
|
loggerConfig.SkipMethods = append(loggerConfig.SkipMethods, config.CustomSkipMethods...)
|
|
loggerConfig.SkipPathPrefixes = append(loggerConfig.SkipPathPrefixes, config.CustomSkipPrefixes...)
|
|
|
|
return FilteredLoggerWithConfig(loggerConfig)
|
|
}
|
|
|
|
// HTTPFilteredLogger returns a filtered logger for the main HTTP server
|
|
func HTTPFilteredLogger() gin.HandlerFunc {
|
|
return UnifiedFilteredLogger("http")
|
|
}
|
|
|
|
// S3FilteredLogger returns a filtered logger for the S3 server
|
|
func S3FilteredLogger() gin.HandlerFunc {
|
|
return UnifiedFilteredLogger("s3")
|
|
} |