refactor(log):Refactor log filtering to use centralized configuration and add server-specific filtering (#798)

* 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
This commit is contained in:
Suyunjing
2025-07-24 16:10:47 +08:00
committed by GitHub
parent 93849a3b5b
commit a9f02ecdac
4 changed files with 77 additions and 150 deletions

View File

@ -44,6 +44,32 @@ type LogConfig struct {
MaxBackups int `json:"max_backups" env:"MAX_BACKUPS"`
MaxAge int `json:"max_age" env:"MAX_AGE"`
Compress bool `json:"compress" env:"COMPRESS"`
Filter LogFilterConfig `json:"filter"` // Log filtering configuration (config file only, no env support)
}
// LogFilterConfig holds configuration for log filtering
// Note: This configuration is only supported via config file, not environment variables
type LogFilterConfig struct {
// EnableFiltering controls whether log filtering is enabled
EnableFiltering bool `json:"enable_filtering"`
// FilterHealthChecks controls whether to filter health check requests
FilterHealthChecks bool `json:"filter_health_checks"`
// FilterWebDAV controls whether to filter WebDAV requests (only for HTTP server)
FilterWebDAV bool `json:"filter_webdav"`
// FilterHEADRequests controls whether to filter HEAD requests
FilterHEADRequests bool `json:"filter_head_requests"`
// CustomSkipPaths allows adding custom paths to skip
CustomSkipPaths []string `json:"custom_skip_paths"`
// CustomSkipMethods allows adding custom methods to skip
CustomSkipMethods []string `json:"custom_skip_methods"`
// CustomSkipPrefixes allows adding custom path prefixes to skip
CustomSkipPrefixes []string `json:"custom_skip_prefixes"`
}
type TaskConfig struct {
@ -152,6 +178,15 @@ func DefaultConfig(dataDir string) *Config {
MaxSize: 50,
MaxBackups: 30,
MaxAge: 28,
Filter: LogFilterConfig{
EnableFiltering: true,
FilterHealthChecks: true,
FilterWebDAV: true,
FilterHEADRequests: true,
CustomSkipPaths: []string{},
CustomSkipMethods: []string{},
CustomSkipPrefixes: []string{},
},
},
MaxConnections: 0,
MaxConcurrency: 64,