option.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package filewriter
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. )
  7. // RotateFormat
  8. const (
  9. RotateDaily = "2006-01-02"
  10. )
  11. var defaultOption = option{
  12. RotateFormat: RotateDaily,
  13. MaxSize: 1 << 30,
  14. ChanSize: 1024 * 8,
  15. RotateInterval: 10 * time.Second,
  16. }
  17. type option struct {
  18. RotateFormat string
  19. MaxFile int
  20. MaxSize int64
  21. ChanSize int
  22. // TODO export Option
  23. RotateInterval time.Duration
  24. WriteTimeout time.Duration
  25. }
  26. // Option filewriter option
  27. type Option func(opt *option)
  28. // RotateFormat e.g 2006-01-02 meaning rotate log file every day.
  29. // NOTE: format can't contain ".", "." will cause panic ヽ(*。>Д<)o゜.
  30. func RotateFormat(format string) Option {
  31. if strings.Contains(format, ".") {
  32. panic(fmt.Sprintf("rotate format can't contain '.' format: %s", format))
  33. }
  34. return func(opt *option) {
  35. opt.RotateFormat = format
  36. }
  37. }
  38. // MaxFile default 999, 0 meaning unlimit.
  39. // TODO: don't create file list if MaxSize is unlimt.
  40. func MaxFile(n int) Option {
  41. return func(opt *option) {
  42. opt.MaxFile = n
  43. }
  44. }
  45. // MaxSize set max size for single log file,
  46. // defult 1GB, 0 meaning unlimit.
  47. func MaxSize(n int64) Option {
  48. return func(opt *option) {
  49. opt.MaxSize = n
  50. }
  51. }
  52. // ChanSize set internal chan size default 8192 use about 64k memory on x64 platfrom static,
  53. // because filewriter has internal object pool, change chan size bigger may cause filewriter use
  54. // a lot of memory, because sync.Pool can't set expire time memory won't free until program exit.
  55. func ChanSize(n int) Option {
  56. return func(opt *option) {
  57. opt.ChanSize = n
  58. }
  59. }