| 21 | ) |
| 22 | |
| 23 | func safeQueryParams(params url.Values) []slog.Field { |
| 24 | if len(params) == 0 { |
| 25 | return nil |
| 26 | } |
| 27 | |
| 28 | fields := make([]slog.Field, 0, len(params)) |
| 29 | for key, values := range params { |
| 30 | // Check if this parameter should be included |
| 31 | for _, pattern := range safeParams { |
| 32 | if strings.EqualFold(key, pattern) { |
| 33 | // Prepend query parameters in the log line to ensure we don't have issues with collisions |
| 34 | // in case any other internal logging fields already log fields with similar names |
| 35 | fieldName := "query_" + key |
| 36 | |
| 37 | // Log the actual values for non-sensitive parameters |
| 38 | if len(values) == 1 { |
| 39 | fields = append(fields, slog.F(fieldName, values[0])) |
| 40 | continue |
| 41 | } |
| 42 | fields = append(fields, slog.F(fieldName, values)) |
| 43 | } |
| 44 | } |
| 45 | // Some query params we just want to log the count of the params length |
| 46 | for _, pattern := range countParams { |
| 47 | if !strings.EqualFold(key, pattern) { |
| 48 | continue |
| 49 | } |
| 50 | count := 0 |
| 51 | |
| 52 | // Prepend query parameters in the log line to ensure we don't have issues with collisions |
| 53 | // in case any other internal logging fields already log fields with similar names |
| 54 | fieldName := "query_" + key |
| 55 | |
| 56 | // Count comma-separated values for CSV format |
| 57 | for _, v := range values { |
| 58 | if strings.Contains(v, ",") { |
| 59 | count += len(strings.Split(v, ",")) |
| 60 | continue |
| 61 | } |
| 62 | count++ |
| 63 | } |
| 64 | // For logging we always want strings |
| 65 | fields = append(fields, slog.F(fieldName+"_count", strconv.Itoa(count))) |
| 66 | } |
| 67 | } |
| 68 | return fields |
| 69 | } |
| 70 | |
| 71 | func Logger(log slog.Logger, hostResolver func(*http.Request) string) func(next http.Handler) http.Handler { |
| 72 | return func(next http.Handler) http.Handler { |