You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.5 KiB

  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. return w.initFd()
  99. }
  100. func (w *FileLogWriter) docheck(size int) {
  101. w.startLock.Lock()
  102. defer w.startLock.Unlock()
  103. if w.Rotate && ((w.Maxlines > 0 && w.maxlinesCurlines >= w.Maxlines) ||
  104. (w.Maxsize > 0 && w.maxsizeCursize >= w.Maxsize) ||
  105. (w.Daily && time.Now().Day() != w.dailyOpenDate)) {
  106. if err := w.DoRotate(); err != nil {
  107. fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
  108. return
  109. }
  110. }
  111. w.maxlinesCurlines++
  112. w.maxsizeCursize += size
  113. }
  114. // WriteMsg writes logger message into file.
  115. func (w *FileLogWriter) WriteMsg(msg string, skip, level int) error {
  116. if level < w.Level {
  117. return nil
  118. }
  119. n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] "
  120. w.docheck(n)
  121. w.Logger.Println(msg)
  122. return nil
  123. }
  124. func (w *FileLogWriter) createLogFile() (*os.File, error) {
  125. // Open the log file
  126. return os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
  127. }
  128. func (w *FileLogWriter) initFd() error {
  129. fd := w.mw.fd
  130. finfo, err := fd.Stat()
  131. if err != nil {
  132. return fmt.Errorf("get stat: %v", err)
  133. }
  134. w.maxsizeCursize = int(finfo.Size())
  135. w.dailyOpenDate = time.Now().Day()
  136. if finfo.Size() > 0 {
  137. content, err := ioutil.ReadFile(w.Filename)
  138. if err != nil {
  139. return err
  140. }
  141. w.maxlinesCurlines = len(strings.Split(string(content), "\n"))
  142. } else {
  143. w.maxlinesCurlines = 0
  144. }
  145. return nil
  146. }
  147. // DoRotate means it need to write file in new file.
  148. // new file name like xx.log.2013-01-01.2
  149. func (w *FileLogWriter) DoRotate() error {
  150. _, err := os.Lstat(w.Filename)
  151. if err == nil { // file exists
  152. // Find the next available number
  153. num := 1
  154. fname := ""
  155. for ; err == nil && num <= 999; num++ {
  156. fname = w.Filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num)
  157. _, err = os.Lstat(fname)
  158. }
  159. // return error if the last file checked still existed
  160. if err == nil {
  161. return fmt.Errorf("rotate: cannot find free log number to rename %s", w.Filename)
  162. }
  163. // block Logger's io.Writer
  164. w.mw.Lock()
  165. defer w.mw.Unlock()
  166. fd := w.mw.fd
  167. fd.Close()
  168. // close fd before rename
  169. // Rename the file to its newfound home
  170. if err = os.Rename(w.Filename, fname); err != nil {
  171. return fmt.Errorf("Rotate: %v", err)
  172. }
  173. // re-start logger
  174. if err = w.StartLogger(); err != nil {
  175. return fmt.Errorf("Rotate StartLogger: %v", err)
  176. }
  177. go w.deleteOldLog()
  178. }
  179. return nil
  180. }
  181. func (w *FileLogWriter) deleteOldLog() {
  182. dir := filepath.Dir(w.Filename)
  183. filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
  184. defer func() {
  185. if r := recover(); r != nil {
  186. returnErr = fmt.Errorf("Unable to delete old log '%s', error: %+v", path, r)
  187. }
  188. }()
  189. if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.Maxdays) {
  190. if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) {
  191. if err := os.Remove(path); err != nil {
  192. returnErr = fmt.Errorf("Failed to remove %s: %v", path, err)
  193. }
  194. }
  195. }
  196. return returnErr
  197. })
  198. }
  199. // Destroy destroy file logger, close file writer.
  200. func (w *FileLogWriter) Destroy() {
  201. w.mw.fd.Close()
  202. }
  203. // Flush flush file logger.
  204. // there are no buffering messages in file logger in memory.
  205. // flush file means sync file from disk.
  206. func (w *FileLogWriter) Flush() {
  207. w.mw.fd.Sync()
  208. }
  209. func init() {
  210. Register("file", NewFileWriter)
  211. }