file.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package hooks
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "github.com/sirupsen/logrus"
  20. )
  21. type LogFileHook struct {
  22. FileDir string
  23. FileName string
  24. fullPath string
  25. file *os.File
  26. written int64
  27. }
  28. func (h *LogFileHook) Init() error {
  29. if fi, err := os.Lstat(h.FileDir); err != nil {
  30. if os.IsNotExist(err) {
  31. os.MkdirAll(h.FileDir, 0755)
  32. } else {
  33. return fmt.Errorf("Lstat %s: %s", h.FileDir, err)
  34. }
  35. } else if !fi.Mode().IsDir() {
  36. return fmt.Errorf("%s exists and it's not a directory", h.FileDir)
  37. }
  38. h.fullPath = filepath.Join(h.FileDir, h.FileName)
  39. file, err := os.OpenFile(h.fullPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755)
  40. if err != nil {
  41. return fmt.Errorf("OpenFile %s: %s", h.fullPath, err)
  42. }
  43. h.file = file
  44. h.written = 0
  45. return nil
  46. }
  47. func (h *LogFileHook) DeInit() {
  48. if h.file != nil {
  49. h.file.Close()
  50. }
  51. }
  52. func (h *LogFileHook) Levels() []logrus.Level {
  53. return logrus.AllLevels
  54. }
  55. func (h *LogFileHook) Fire(e *logrus.Entry) error {
  56. if b, err := e.Logger.Formatter.Format(e); err != nil {
  57. return err
  58. } else {
  59. n, err := h.file.Write(b)
  60. h.written += int64(n)
  61. return err
  62. }
  63. return nil
  64. }
  65. func (h *LogFileHook) Written() int64 {
  66. return h.written
  67. }
  68. // rotate by size
  69. type LogFileRotateHook struct {
  70. LogFileHook
  71. RotateNum int
  72. RotateSize int64
  73. filePaths []string
  74. }
  75. func (h *LogFileRotateHook) Init() error {
  76. if err := h.LogFileHook.Init(); err != nil {
  77. return err
  78. }
  79. h.filePaths = make([]string, h.RotateNum)
  80. for i := 1; i < h.RotateNum; i++ {
  81. fileName := fmt.Sprintf("%s.%d", h.FileName, i)
  82. filePath := filepath.Join(h.FileDir, fileName)
  83. h.filePaths[i] = filePath
  84. }
  85. h.filePaths[0] = filepath.Join(h.FileDir, h.FileName)
  86. return nil
  87. }
  88. func (h *LogFileRotateHook) rotate() {
  89. for i := h.RotateNum - 1; i > 0; i-- {
  90. filePath0 := h.filePaths[i-1]
  91. if _, err := os.Lstat(filePath0); err != nil {
  92. continue
  93. }
  94. filePath1 := h.filePaths[i]
  95. os.Rename(filePath0, filePath1)
  96. }
  97. h.LogFileHook.DeInit()
  98. h.LogFileHook.Init()
  99. }
  100. func (h *LogFileRotateHook) Fire(e *logrus.Entry) error {
  101. if err := h.LogFileHook.Fire(e); err != nil {
  102. return err
  103. }
  104. if h.LogFileHook.Written() >= h.RotateSize {
  105. h.rotate()
  106. }
  107. return nil
  108. }