Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

file.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package log
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io/ioutil"
  10. "log"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "sync"
  15. "time"
  16. )
  17. // FileLogWriter implements LoggerInterface.
  18. // It writes messages by lines limit, file size limit, or time frequency.
  19. type FileLogWriter struct {
  20. *log.Logger
  21. mw *MuxWriter
  22. // The opened file
  23. Filename string `json:"filename"`
  24. Maxlines int `json:"maxlines"`
  25. maxlinesCurlines int
  26. // Rotate at size
  27. Maxsize int `json:"maxsize"`
  28. maxsizeCursize int
  29. // Rotate daily
  30. Daily bool `json:"daily"`
  31. Maxdays int64 `json:"maxdays"`
  32. dailyOpenDate int
  33. Rotate bool `json:"rotate"`
  34. startLock sync.Mutex // Only one log can write to the file
  35. Level int `json:"level"`
  36. }
  37. // MuxWriter an *os.File writer with locker.
  38. type MuxWriter struct {
  39. sync.Mutex
  40. fd *os.File
  41. }
  42. // Write writes to os.File.
  43. func (l *MuxWriter) Write(b []byte) (int, error) {
  44. l.Lock()
  45. defer l.Unlock()
  46. return l.fd.Write(b)
  47. }
  48. // SetFd sets os.File in writer.
  49. func (l *MuxWriter) SetFd(fd *os.File) {
  50. if l.fd != nil {
  51. l.fd.Close()
  52. }
  53. l.fd = fd
  54. }
  55. // NewFileWriter create a FileLogWriter returning as LoggerInterface.
  56. func NewFileWriter() LoggerInterface {
  57. w := &FileLogWriter{
  58. Filename: "",
  59. Maxlines: 1000000,
  60. Maxsize: 1 << 28, //256 MB
  61. Daily: true,
  62. Maxdays: 7,
  63. Rotate: true,
  64. Level: TRACE,
  65. }
  66. // use MuxWriter instead direct use os.File for lock write when rotate
  67. w.mw = new(MuxWriter)
  68. // set MuxWriter as Logger's io.Writer
  69. w.Logger = log.New(w.mw, "", log.Ldate|log.Ltime)
  70. return w
  71. }
  72. // Init file logger with json config.
  73. // config like:
  74. // {
  75. // "filename":"log/gogs.log",
  76. // "maxlines":10000,
  77. // "maxsize":1<<30,
  78. // "daily":true,
  79. // "maxdays":15,
  80. // "rotate":true
  81. // }
  82. func (w *FileLogWriter) Init(config string) error {
  83. if err := json.Unmarshal([]byte(config), w); err != nil {
  84. return err
  85. }
  86. if len(w.Filename) == 0 {
  87. return errors.New("config must have filename")
  88. }
  89. return w.StartLogger()
  90. }
  91. // StartLogger start file logger. create log file and set to locker-inside file writer.
  92. func (w *FileLogWriter) StartLogger() error {
  93. fd, err := w.createLogFile()
  94. if err != nil {
  95. return err
  96. }
  97. w.mw.SetFd(fd)
  98. if err = w.initFd(); err != nil {
  99. return err
  100. }
  101. return nil
  102. }
  103. func (w *FileLogWriter) docheck(size int) {
  104. w.startLock.Lock()
  105. defer w.startLock.Unlock()
  106. if w.Rotate && ((w.Maxlines > 0 && w.maxlinesCurlines >= w.Maxlines) ||
  107. (w.Maxsize > 0 && w.maxsizeCursize >= w.Maxsize) ||
  108. (w.Daily && time.Now().Day() != w.dailyOpenDate)) {
  109. if err := w.DoRotate(); err != nil {
  110. fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
  111. return
  112. }
  113. }
  114. w.maxlinesCurlines++
  115. w.maxsizeCursize += size
  116. }
  117. // WriteMsg writes logger message into file.
  118. func (w *FileLogWriter) WriteMsg(msg string, skip, level int) error {
  119. if level < w.Level {
  120. return nil
  121. }
  122. n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] "
  123. w.docheck(n)
  124. w.Logger.Println(msg)
  125. return nil
  126. }
  127. func (w *FileLogWriter) createLogFile() (*os.File, error) {
  128. // Open the log file
  129. return os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
  130. }
  131. func (w *FileLogWriter) initFd() error {
  132. fd := w.mw.fd
  133. finfo, err := fd.Stat()
  134. if err != nil {
  135. return fmt.Errorf("get stat: %v", err)
  136. }
  137. w.maxsizeCursize = int(finfo.Size())
  138. w.dailyOpenDate = time.Now().Day()
  139. if finfo.Size() > 0 {
  140. content, err := ioutil.ReadFile(w.Filename)
  141. if err != nil {
  142. return err
  143. }
  144. w.maxlinesCurlines = len(strings.Split(string(content), "\n"))
  145. } else {
  146. w.maxlinesCurlines = 0
  147. }
  148. return nil
  149. }
  150. // DoRotate means it need to write file in new file.
  151. // new file name like xx.log.2013-01-01.2
  152. func (w *FileLogWriter) DoRotate() error {
  153. _, err := os.Lstat(w.Filename)
  154. if err == nil { // file exists
  155. // Find the next available number
  156. num := 1
  157. fname := ""
  158. for ; err == nil && num <= 999; num++ {
  159. fname = w.Filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num)
  160. _, err = os.Lstat(fname)
  161. }
  162. // return error if the last file checked still existed
  163. if err == nil {
  164. return fmt.Errorf("rotate: cannot find free log number to rename %s", w.Filename)
  165. }
  166. // block Logger's io.Writer
  167. w.mw.Lock()
  168. defer w.mw.Unlock()
  169. fd := w.mw.fd
  170. fd.Close()
  171. // close fd before rename
  172. // Rename the file to its newfound home
  173. if err = os.Rename(w.Filename, fname); err != nil {
  174. return fmt.Errorf("Rotate: %v", err)
  175. }
  176. // re-start logger
  177. if err = w.StartLogger(); err != nil {
  178. return fmt.Errorf("Rotate StartLogger: %v", err)
  179. }
  180. go w.deleteOldLog()
  181. }
  182. return nil
  183. }
  184. func (w *FileLogWriter) deleteOldLog() {
  185. dir := filepath.Dir(w.Filename)
  186. filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
  187. defer func() {
  188. if r := recover(); r != nil {
  189. returnErr = fmt.Errorf("Unable to delete old log '%s', error: %+v", path, r)
  190. }
  191. }()
  192. if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.Maxdays) {
  193. if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) {
  194. if err := os.Remove(path); err != nil {
  195. returnErr = fmt.Errorf("Failed to remove %s: %v", path, err)
  196. }
  197. }
  198. }
  199. return returnErr
  200. })
  201. }
  202. // Destroy destroy file logger, close file writer.
  203. func (w *FileLogWriter) Destroy() {
  204. w.mw.fd.Close()
  205. }
  206. // Flush flush file logger.
  207. // there are no buffering messages in file logger in memory.
  208. // flush file means sync file from disk.
  209. func (w *FileLogWriter) Flush() {
  210. w.mw.fd.Sync()
  211. }
  212. func init() {
  213. Register("file", NewFileWriter)
  214. }