log_cache_management.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package nginx_log
  2. import (
  3. "sync"
  4. )
  5. var (
  6. // logCache is the map to store all found log files
  7. logCache = make(map[string]*NginxLogCache)
  8. cacheMutex sync.RWMutex
  9. )
  10. // AddLogPath adds a log path to the log cache with the source config file
  11. func AddLogPath(path, logType, name, configFile string) {
  12. cacheMutex.Lock()
  13. defer cacheMutex.Unlock()
  14. logCache[path] = &NginxLogCache{
  15. Path: path,
  16. Type: logType,
  17. Name: name,
  18. ConfigFile: configFile,
  19. }
  20. }
  21. // RemoveLogPathsFromConfig removes all log paths associated with a specific config file
  22. func RemoveLogPathsFromConfig(configFile string) {
  23. cacheMutex.Lock()
  24. defer cacheMutex.Unlock()
  25. for path, logEntry := range logCache {
  26. if logEntry.ConfigFile == configFile {
  27. delete(logCache, path)
  28. }
  29. }
  30. }
  31. // GetAllLogPaths returns all cached log paths, optionally filtered
  32. func GetAllLogPaths(filters ...func(*NginxLogCache) bool) []*NginxLogCache {
  33. cacheMutex.RLock()
  34. defer cacheMutex.RUnlock()
  35. var logs []*NginxLogCache
  36. for _, logEntry := range logCache {
  37. // Apply all filters
  38. include := true
  39. for _, filter := range filters {
  40. if !filter(logEntry) {
  41. include = false
  42. break
  43. }
  44. }
  45. if include {
  46. logs = append(logs, logEntry)
  47. }
  48. }
  49. return logs
  50. }
  51. // ClearLogCache clears the entire log cache
  52. func ClearLogCache() {
  53. cacheMutex.Lock()
  54. defer cacheMutex.Unlock()
  55. logCache = make(map[string]*NginxLogCache)
  56. }