interface.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package rotatelogs
  2. import (
  3. "os"
  4. "sync"
  5. "time"
  6. strftime "github.com/lestrrat-go/strftime"
  7. )
  8. type Handler interface {
  9. Handle(Event)
  10. }
  11. type HandlerFunc func(Event)
  12. type Event interface {
  13. Type() EventType
  14. }
  15. type EventType int
  16. const (
  17. InvalidEventType EventType = iota
  18. FileRotatedEventType
  19. )
  20. type FileRotatedEvent struct {
  21. prev string // previous filename
  22. current string // current, new filename
  23. }
  24. // RotateLogs represents a log file that gets
  25. // automatically rotated as you write to it.
  26. type RotateLogs struct {
  27. clock Clock
  28. curFn string
  29. curBaseFn string
  30. globPattern string
  31. generation int
  32. linkName string
  33. maxAge time.Duration
  34. mutex sync.RWMutex
  35. eventHandler Handler
  36. outFh *os.File
  37. pattern *strftime.Strftime
  38. rotationTime time.Duration
  39. rotationSize int64
  40. rotationCount uint
  41. forceNewFile bool
  42. }
  43. // Clock is the interface used by the RotateLogs
  44. // object to determine the current time
  45. type Clock interface {
  46. Now() time.Time
  47. }
  48. type clockFn func() time.Time
  49. // UTC is an object satisfying the Clock interface, which
  50. // returns the current time in UTC
  51. var UTC = clockFn(func() time.Time { return time.Now().UTC() })
  52. // Local is an object satisfying the Clock interface, which
  53. // returns the current time in the local timezone
  54. var Local = clockFn(time.Now)
  55. // Option is used to pass optional arguments to
  56. // the RotateLogs constructor
  57. type Option interface {
  58. Name() string
  59. Value() interface{}
  60. }