1
0

log_cache.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package nginx_log
  2. import (
  3. "sync"
  4. )
  5. // NginxLogCache represents a cached log entry from nginx configuration
  6. type NginxLogCache struct {
  7. Path string `json:"path"` // Path to the log file
  8. Type string `json:"type"` // Type of log: "access" or "error"
  9. Name string `json:"name"` // Name of the log file
  10. }
  11. var (
  12. // logCache is the map to store all found log files
  13. logCache map[string]*NginxLogCache
  14. cacheMutex sync.RWMutex
  15. )
  16. func init() {
  17. // Initialize the cache
  18. logCache = make(map[string]*NginxLogCache)
  19. }
  20. // AddLogPath adds a log path to the log cache
  21. func AddLogPath(path, logType, name string) {
  22. cacheMutex.Lock()
  23. defer cacheMutex.Unlock()
  24. logCache[path] = &NginxLogCache{
  25. Path: path,
  26. Type: logType,
  27. Name: name,
  28. }
  29. }
  30. // GetAllLogPaths returns all cached log paths
  31. func GetAllLogPaths(filters ...func(*NginxLogCache) bool) []*NginxLogCache {
  32. cacheMutex.RLock()
  33. defer cacheMutex.RUnlock()
  34. result := make([]*NginxLogCache, 0, len(logCache))
  35. for _, cache := range logCache {
  36. flag := true
  37. if len(filters) > 0 {
  38. for _, filter := range filters {
  39. if !filter(cache) {
  40. flag = false
  41. break
  42. }
  43. }
  44. }
  45. if flag {
  46. result = append(result, cache)
  47. }
  48. }
  49. return result
  50. }
  51. // ClearLogCache clears all entries in the log cache
  52. func ClearLogCache() {
  53. cacheMutex.Lock()
  54. defer cacheMutex.Unlock()
  55. // Clear the cache
  56. logCache = make(map[string]*NginxLogCache)
  57. }