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.

237 lines
5.3 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. maxlines_curlines int
  26. // Rotate at size
  27. Maxsize int `json:"maxsize"`
  28. maxsize_cursize int
  29. // Rotate daily
  30. Daily bool `json:"daily"`
  31. Maxdays int64 `json:"maxdays`
  32. daily_opendate 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. // an *os.File writer with locker.
  38. type MuxWriter struct {
  39. sync.Mutex
  40. fd *os.File
  41. }
  42. // write 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. // set 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. // 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. // 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.maxlines_curlines >= w.Maxlines) ||
  107. (w.Maxsize > 0 && w.maxsize_cursize >= w.Maxsize) ||
  108. (w.Daily && time.Now().Day() != w.daily_opendate)) {
  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.maxlines_curlines++
  115. w.maxsize_cursize += size
  116. }
  117. // write 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: %s\n", err)
  136. }
  137. w.maxsize_cursize = int(finfo.Size())
  138. w.daily_opendate = 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.maxlines_curlines = len(strings.Split(string(content), "\n"))
  145. } else {
  146. w.maxlines_curlines = 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\n", 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: %s\n", err)
  175. }
  176. // re-start logger
  177. if err = w.StartLogger(); err != nil {
  178. return fmt.Errorf("Rotate StartLogger: %s\n", 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) error {
  187. if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.Maxdays) {
  188. if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) {
  189. os.Remove(path)
  190. }
  191. }
  192. return nil
  193. })
  194. }
  195. // destroy file logger, close file writer.
  196. func (w *FileLogWriter) Destroy() {
  197. w.mw.fd.Close()
  198. }
  199. // flush file logger.
  200. // there are no buffering messages in file logger in memory.
  201. // flush file means sync file from disk.
  202. func (w *FileLogWriter) Flush() {
  203. w.mw.fd.Sync()
  204. }
  205. func init() {
  206. Register("file", NewFileWriter)
  207. }