rotatefilewriter.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package loggerutils
  2. import (
  3. "os"
  4. "strconv"
  5. "sync"
  6. "github.com/docker/docker/pkg/pubsub"
  7. )
  8. // RotateFileWriter is Logger implementation for default Docker logging.
  9. type RotateFileWriter struct {
  10. f *os.File // store for closing
  11. mu sync.Mutex
  12. capacity int64 //maximum size of each file
  13. maxFiles int //maximum number of files
  14. notifyRotate *pubsub.Publisher
  15. }
  16. //NewRotateFileWriter creates new RotateFileWriter
  17. func NewRotateFileWriter(logPath string, capacity int64, maxFiles int) (*RotateFileWriter, error) {
  18. log, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640)
  19. if err != nil {
  20. return &RotateFileWriter{}, err
  21. }
  22. return &RotateFileWriter{
  23. f: log,
  24. capacity: capacity,
  25. maxFiles: maxFiles,
  26. notifyRotate: pubsub.NewPublisher(0, 1),
  27. }, nil
  28. }
  29. //WriteLog write log messge to File
  30. func (w *RotateFileWriter) Write(message []byte) (int, error) {
  31. w.mu.Lock()
  32. defer w.mu.Unlock()
  33. if err := w.checkCapacityAndRotate(); err != nil {
  34. return -1, err
  35. }
  36. return w.f.Write(message)
  37. }
  38. func (w *RotateFileWriter) checkCapacityAndRotate() error {
  39. if w.capacity == -1 {
  40. return nil
  41. }
  42. meta, err := w.f.Stat()
  43. if err != nil {
  44. return err
  45. }
  46. if meta.Size() >= w.capacity {
  47. name := w.f.Name()
  48. if err := w.f.Close(); err != nil {
  49. return err
  50. }
  51. if err := rotate(name, w.maxFiles); err != nil {
  52. return err
  53. }
  54. file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 06400)
  55. if err != nil {
  56. return err
  57. }
  58. w.f = file
  59. w.notifyRotate.Publish(struct{}{})
  60. }
  61. return nil
  62. }
  63. func rotate(name string, maxFiles int) error {
  64. if maxFiles < 2 {
  65. return nil
  66. }
  67. for i := maxFiles - 1; i > 1; i-- {
  68. toPath := name + "." + strconv.Itoa(i)
  69. fromPath := name + "." + strconv.Itoa(i-1)
  70. if err := backup(fromPath, toPath); err != nil && !os.IsNotExist(err) {
  71. return err
  72. }
  73. }
  74. if err := backup(name, name+".1"); err != nil {
  75. return err
  76. }
  77. return nil
  78. }
  79. // backup renames a file from fromPath to toPath
  80. func backup(fromPath, toPath string) error {
  81. if _, err := os.Stat(fromPath); os.IsNotExist(err) {
  82. return err
  83. }
  84. if _, err := os.Stat(toPath); !os.IsNotExist(err) {
  85. err := os.Remove(toPath)
  86. if err != nil {
  87. return err
  88. }
  89. }
  90. return os.Rename(fromPath, toPath)
  91. }
  92. // LogPath returns the location the given wirter logs to.
  93. func (w *RotateFileWriter) LogPath() string {
  94. return w.f.Name()
  95. }
  96. // MaxFiles return maximum number of files
  97. func (w *RotateFileWriter) MaxFiles() int {
  98. return w.maxFiles
  99. }
  100. //NotifyRotate returns the new subscriber
  101. func (w *RotateFileWriter) NotifyRotate() chan interface{} {
  102. return w.notifyRotate.Subscribe()
  103. }
  104. //NotifyRotateEvict removes the specified subscriber from receiving any more messages.
  105. func (w *RotateFileWriter) NotifyRotateEvict(sub chan interface{}) {
  106. w.notifyRotate.Evict(sub)
  107. }
  108. // Close closes underlying file and signals all readers to stop.
  109. func (w *RotateFileWriter) Close() error {
  110. return w.f.Close()
  111. }