options.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package rotatelogs
  2. import (
  3. "time"
  4. "github.com/lestrrat-go/file-rotatelogs/internal/option"
  5. )
  6. const (
  7. optkeyClock = "clock"
  8. optkeyHandler = "handler"
  9. optkeyLinkName = "link-name"
  10. optkeyMaxAge = "max-age"
  11. optkeyRotationTime = "rotation-time"
  12. optkeyRotationSize = "rotation-size"
  13. optkeyRotationCount = "rotation-count"
  14. optkeyForceNewFile = "force-new-file"
  15. )
  16. // WithClock creates a new Option that sets a clock
  17. // that the RotateLogs object will use to determine
  18. // the current time.
  19. //
  20. // By default rotatelogs.Local, which returns the
  21. // current time in the local time zone, is used. If you
  22. // would rather use UTC, use rotatelogs.UTC as the argument
  23. // to this option, and pass it to the constructor.
  24. func WithClock(c Clock) Option {
  25. return option.New(optkeyClock, c)
  26. }
  27. // WithLocation creates a new Option that sets up a
  28. // "Clock" interface that the RotateLogs object will use
  29. // to determine the current time.
  30. //
  31. // This optin works by always returning the in the given
  32. // location.
  33. func WithLocation(loc *time.Location) Option {
  34. return option.New(optkeyClock, clockFn(func() time.Time {
  35. return time.Now().In(loc)
  36. }))
  37. }
  38. // WithLinkName creates a new Option that sets the
  39. // symbolic link name that gets linked to the current
  40. // file name being used.
  41. func WithLinkName(s string) Option {
  42. return option.New(optkeyLinkName, s)
  43. }
  44. // WithMaxAge creates a new Option that sets the
  45. // max age of a log file before it gets purged from
  46. // the file system.
  47. func WithMaxAge(d time.Duration) Option {
  48. return option.New(optkeyMaxAge, d)
  49. }
  50. // WithRotationTime creates a new Option that sets the
  51. // time between rotation.
  52. func WithRotationTime(d time.Duration) Option {
  53. return option.New(optkeyRotationTime, d)
  54. }
  55. // WithRotationSize creates a new Option that sets the
  56. // log file size between rotation.
  57. func WithRotationSize(s int64) Option {
  58. return option.New(optkeyRotationSize, s)
  59. }
  60. // WithRotationCount creates a new Option that sets the
  61. // number of files should be kept before it gets
  62. // purged from the file system.
  63. func WithRotationCount(n uint) Option {
  64. return option.New(optkeyRotationCount, n)
  65. }
  66. // WithHandler creates a new Option that specifies the
  67. // Handler object that gets invoked when an event occurs.
  68. // Currently `FileRotated` event is supported
  69. func WithHandler(h Handler) Option {
  70. return option.New(optkeyHandler, h)
  71. }
  72. // ForceNewFile ensures a new file is created every time New()
  73. // is called. If the base file name already exists, an implicit
  74. // rotation is performed
  75. func ForceNewFile() Option {
  76. return option.New(optkeyForceNewFile, true)
  77. }