1
1
package middlewares
2
2
3
3
import (
4
- "os"
5
- "strings"
6
-
4
+ "github.com/OpenListTeam/OpenList/v4/internal/conf"
7
5
"github.com/gin-gonic/gin"
8
6
log "github.com/sirupsen/logrus"
9
7
)
10
8
11
- // LogFilterConfig holds configuration for log filtering
12
- type LogFilterConfig struct {
13
- // EnableFiltering controls whether log filtering is enabled
14
- EnableFiltering bool
15
-
16
- // FilterHealthChecks controls whether to filter health check requests
17
- FilterHealthChecks bool
18
-
19
- // FilterWebDAV controls whether to filter WebDAV requests
20
- FilterWebDAV bool
21
-
22
- // FilterHEADRequests controls whether to filter HEAD requests
23
- FilterHEADRequests bool
24
-
25
- // CustomSkipPaths allows adding custom paths to skip
26
- CustomSkipPaths []string
27
-
28
- // CustomSkipMethods allows adding custom methods to skip
29
- CustomSkipMethods []string
9
+ // UnifiedFilteredLogger returns a filtered logger using global configuration
10
+ // serverType: "http" for main HTTP server, "s3" for S3 server
11
+ func UnifiedFilteredLogger (serverType string ) gin.HandlerFunc {
12
+ config := conf .Conf .Log .Filter
30
13
31
- // CustomSkipPrefixes allows adding custom path prefixes to skip
32
- CustomSkipPrefixes []string
33
- }
34
-
35
- // DefaultLogFilterConfig returns the default configuration
36
- func DefaultLogFilterConfig () LogFilterConfig {
37
- return LogFilterConfig {
38
- EnableFiltering : true ,
39
- FilterHealthChecks : true ,
40
- FilterWebDAV : true ,
41
- FilterHEADRequests : true ,
42
- CustomSkipPaths : []string {},
43
- CustomSkipMethods : []string {},
44
- CustomSkipPrefixes : []string {},
45
- }
46
- }
47
-
48
- // LoadLogFilterConfigFromEnv loads configuration from environment variables
49
- func LoadLogFilterConfigFromEnv () LogFilterConfig {
50
- config := DefaultLogFilterConfig ()
51
-
52
- // Check if filtering is enabled
53
- if env := os .Getenv ("OPENLIST_LOG_FILTER_ENABLED" ); env != "" {
54
- config .EnableFiltering = strings .ToLower (env ) == "true"
55
- }
56
-
57
- // Check individual filter options
58
- if env := os .Getenv ("OPENLIST_LOG_FILTER_HEALTH_CHECKS" ); env != "" {
59
- config .FilterHealthChecks = strings .ToLower (env ) == "true"
60
- }
61
-
62
- if env := os .Getenv ("OPENLIST_LOG_FILTER_WEBDAV" ); env != "" {
63
- config .FilterWebDAV = strings .ToLower (env ) == "true"
64
- }
65
-
66
- if env := os .Getenv ("OPENLIST_LOG_FILTER_HEAD_REQUESTS" ); env != "" {
67
- config .FilterHEADRequests = strings .ToLower (env ) == "true"
68
- }
69
-
70
- // Load custom skip paths
71
- if env := os .Getenv ("OPENLIST_LOG_FILTER_SKIP_PATHS" ); env != "" {
72
- config .CustomSkipPaths = strings .Split (env , "," )
73
- for i , path := range config .CustomSkipPaths {
74
- config .CustomSkipPaths [i ] = strings .TrimSpace (path )
75
- }
76
- }
77
-
78
- // Load custom skip methods
79
- if env := os .Getenv ("OPENLIST_LOG_FILTER_SKIP_METHODS" ); env != "" {
80
- config .CustomSkipMethods = strings .Split (env , "," )
81
- for i , method := range config .CustomSkipMethods {
82
- config .CustomSkipMethods [i ] = strings .TrimSpace (strings .ToUpper (method ))
83
- }
84
- }
85
-
86
- // Load custom skip prefixes
87
- if env := os .Getenv ("OPENLIST_LOG_FILTER_SKIP_PREFIXES" ); env != "" {
88
- config .CustomSkipPrefixes = strings .Split (env , "," )
89
- for i , prefix := range config .CustomSkipPrefixes {
90
- config .CustomSkipPrefixes [i ] = strings .TrimSpace (prefix )
91
- }
92
- }
93
-
94
- return config
95
- }
96
-
97
- // ToFilteredLoggerConfig converts LogFilterConfig to FilteredLoggerConfig
98
- func (c LogFilterConfig ) ToFilteredLoggerConfig () FilteredLoggerConfig {
99
- if ! c .EnableFiltering {
100
- // Return empty config to disable filtering
101
- return FilteredLoggerConfig {
102
- Output : log .StandardLogger ().Out ,
103
- }
14
+ if ! config .EnableFiltering {
15
+ // Return standard Gin logger if filtering is disabled
16
+ return gin .LoggerWithWriter (log .StandardLogger ().Out )
104
17
}
105
18
106
- config := FilteredLoggerConfig {
19
+ loggerConfig := FilteredLoggerConfig {
107
20
Output : log .StandardLogger ().Out ,
108
21
}
109
22
110
23
// Add health check paths
111
- if c .FilterHealthChecks {
112
- config .SkipPaths = append (config .SkipPaths , "/ping" )
24
+ if config .FilterHealthChecks {
25
+ loggerConfig .SkipPaths = append (loggerConfig .SkipPaths , "/ping" )
113
26
}
114
27
115
28
// Add HEAD method filtering
116
- if c .FilterHEADRequests {
117
- config .SkipMethods = append (config .SkipMethods , "HEAD" )
29
+ if config .FilterHEADRequests {
30
+ loggerConfig .SkipMethods = append (loggerConfig .SkipMethods , "HEAD" )
118
31
}
119
32
120
- // Add WebDAV filtering
121
- if c .FilterWebDAV {
122
- config .SkipPathPrefixes = append (config .SkipPathPrefixes , "/dav/" )
123
- config .SkipMethods = append (config .SkipMethods , "PROPFIND" )
33
+ // Add WebDAV filtering only for HTTP server (not for S3)
34
+ if config .FilterWebDAV && serverType == "http" {
35
+ loggerConfig .SkipPathPrefixes = append (loggerConfig .SkipPathPrefixes , "/dav/" )
36
+ loggerConfig .SkipMethods = append (loggerConfig .SkipMethods , "PROPFIND" )
124
37
}
125
38
126
39
// Add custom configurations
127
- config .SkipPaths = append (config .SkipPaths , c .CustomSkipPaths ... )
128
- config .SkipMethods = append (config .SkipMethods , c .CustomSkipMethods ... )
129
- config .SkipPathPrefixes = append (config .SkipPathPrefixes , c .CustomSkipPrefixes ... )
40
+ loggerConfig .SkipPaths = append (loggerConfig .SkipPaths , config .CustomSkipPaths ... )
41
+ loggerConfig .SkipMethods = append (loggerConfig .SkipMethods , config .CustomSkipMethods ... )
42
+ loggerConfig .SkipPathPrefixes = append (loggerConfig .SkipPathPrefixes , config .CustomSkipPrefixes ... )
130
43
131
- return config
44
+ return FilteredLoggerWithConfig ( loggerConfig )
132
45
}
133
46
134
- // ConfigurableFilteredLogger returns a filtered logger with configuration loaded from environment
135
- func ConfigurableFilteredLogger () gin.HandlerFunc {
136
- config := LoadLogFilterConfigFromEnv ()
137
- loggerConfig := config .ToFilteredLoggerConfig ()
138
-
139
- if ! config .EnableFiltering {
140
- // Return standard Gin logger if filtering is disabled
141
- return gin .LoggerWithWriter (log .StandardLogger ().Out )
142
- }
143
-
144
- return FilteredLoggerWithConfig (loggerConfig )
47
+ // HTTPFilteredLogger returns a filtered logger for the main HTTP server
48
+ func HTTPFilteredLogger () gin.HandlerFunc {
49
+ return UnifiedFilteredLogger ("http" )
50
+ }
51
+
52
+ // S3FilteredLogger returns a filtered logger for the S3 server
53
+ func S3FilteredLogger () gin.HandlerFunc {
54
+ return UnifiedFilteredLogger ("s3" )
145
55
}
0 commit comments