1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-25 10:04:27 +00:00
gitea/modules/log/file.go

227 lines
5.1 KiB
Go
Raw Normal View History

2014-07-26 04:24:27 +00:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package log
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// FileLogWriter implements LoggerInterface.
// It writes messages by lines limit, file size limit, or time frequency.
type FileLogWriter struct {
*log.Logger
mw *MuxWriter
// The opened file
Filename string `json:"filename"`
// Rotate at size
2016-11-26 11:53:29 +00:00
Maxsize int `json:"maxsize"`
maxsizeCursize int
2014-07-26 04:24:27 +00:00
// Rotate daily
2016-11-26 11:53:29 +00:00
Daily bool `json:"daily"`
Maxdays int64 `json:"maxdays"`
dailyOpenDate int
2014-07-26 04:24:27 +00:00
Rotate bool `json:"rotate"`
startLock sync.Mutex // Only one log can write to the file
Level int `json:"level"`
}
2016-11-26 11:53:29 +00:00
// MuxWriter an *os.File writer with locker.
2014-07-26 04:24:27 +00:00
type MuxWriter struct {
sync.Mutex
fd *os.File
}
2016-11-26 11:53:29 +00:00
// Write writes to os.File.
2014-07-26 04:24:27 +00:00
func (l *MuxWriter) Write(b []byte) (int, error) {
l.Lock()
defer l.Unlock()
return l.fd.Write(b)
}
2016-11-26 11:53:29 +00:00
// SetFd sets os.File in writer.
2014-07-26 04:24:27 +00:00
func (l *MuxWriter) SetFd(fd *os.File) {
if l.fd != nil {
l.fd.Close()
}
l.fd = fd
}
2016-11-26 11:53:29 +00:00
// NewFileWriter create a FileLogWriter returning as LoggerInterface.
2014-07-26 04:24:27 +00:00
func NewFileWriter() LoggerInterface {
w := &FileLogWriter{
Filename: "",
Maxsize: 1 << 28, //256 MB
Daily: true,
Maxdays: 7,
Rotate: true,
Level: TRACE,
}
// use MuxWriter instead direct use os.File for lock write when rotate
w.mw = new(MuxWriter)
// set MuxWriter as Logger's io.Writer
w.Logger = log.New(w.mw, "", log.Ldate|log.Ltime)
return w
}
// Init file logger with json config.
// config like:
// {
// "filename":"log/gogs.log",
// "maxsize":1<<30,
// "daily":true,
// "maxdays":15,
// "rotate":true
// }
func (w *FileLogWriter) Init(config string) error {
if err := json.Unmarshal([]byte(config), w); err != nil {
return err
}
if len(w.Filename) == 0 {
return errors.New("config must have filename")
}
return w.StartLogger()
}
2016-11-26 11:53:29 +00:00
// StartLogger start file logger. create log file and set to locker-inside file writer.
2014-07-26 04:24:27 +00:00
func (w *FileLogWriter) StartLogger() error {
fd, err := w.createLogFile()
if err != nil {
return err
}
w.mw.SetFd(fd)
2017-09-19 08:08:30 +00:00
return w.initFd()
2014-07-26 04:24:27 +00:00
}
func (w *FileLogWriter) docheck(size int) {
w.startLock.Lock()
defer w.startLock.Unlock()
if w.Rotate && ((w.Maxsize > 0 && w.maxsizeCursize >= w.Maxsize) ||
2016-11-26 11:53:29 +00:00
(w.Daily && time.Now().Day() != w.dailyOpenDate)) {
2014-07-26 04:24:27 +00:00
if err := w.DoRotate(); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
return
}
}
2016-11-26 11:53:29 +00:00
w.maxsizeCursize += size
2014-07-26 04:24:27 +00:00
}
2016-11-26 11:53:29 +00:00
// WriteMsg writes logger message into file.
2014-07-26 04:24:27 +00:00
func (w *FileLogWriter) WriteMsg(msg string, skip, level int) error {
if level < w.Level {
return nil
}
n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] "
w.docheck(n)
w.Logger.Println(msg)
return nil
}
func (w *FileLogWriter) createLogFile() (*os.File, error) {
// Open the log file
return os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
}
func (w *FileLogWriter) initFd() error {
fd := w.mw.fd
finfo, err := fd.Stat()
if err != nil {
return fmt.Errorf("get stat: %v", err)
2014-07-26 04:24:27 +00:00
}
2016-11-26 11:53:29 +00:00
w.maxsizeCursize = int(finfo.Size())
w.dailyOpenDate = time.Now().Day()
2014-07-26 04:24:27 +00:00
return nil
}
// DoRotate means it need to write file in new file.
// new file name like xx.log.2013-01-01.2
func (w *FileLogWriter) DoRotate() error {
_, err := os.Lstat(w.Filename)
if err == nil { // file exists
// Find the next available number
num := 1
fname := ""
for ; err == nil && num <= 999; num++ {
fname = w.Filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num)
_, err = os.Lstat(fname)
}
// return error if the last file checked still existed
if err == nil {
2016-11-26 11:53:29 +00:00
return fmt.Errorf("rotate: cannot find free log number to rename %s", w.Filename)
2014-07-26 04:24:27 +00:00
}
// block Logger's io.Writer
w.mw.Lock()
defer w.mw.Unlock()
fd := w.mw.fd
fd.Close()
// close fd before rename
// Rename the file to its newfound home
if err = os.Rename(w.Filename, fname); err != nil {
return fmt.Errorf("Rotate: %v", err)
2014-07-26 04:24:27 +00:00
}
// re-start logger
if err = w.StartLogger(); err != nil {
return fmt.Errorf("Rotate StartLogger: %v", err)
2014-07-26 04:24:27 +00:00
}
go w.deleteOldLog()
}
return nil
}
func (w *FileLogWriter) deleteOldLog() {
dir := filepath.Dir(w.Filename)
2014-08-27 08:39:36 +00:00
filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
defer func() {
if r := recover(); r != nil {
returnErr = fmt.Errorf("Unable to delete old log '%s', error: %+v", path, r)
}
}()
2014-07-26 04:24:27 +00:00
if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.Maxdays) {
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) {
2016-11-30 23:56:15 +00:00
if err := os.Remove(path); err != nil {
returnErr = fmt.Errorf("Failed to remove %s: %v", path, err)
2016-11-30 23:56:15 +00:00
}
2014-07-26 04:24:27 +00:00
}
}
2014-08-27 08:39:36 +00:00
return returnErr
2014-07-26 04:24:27 +00:00
})
}
2016-11-26 11:53:29 +00:00
// Destroy destroy file logger, close file writer.
2014-07-26 04:24:27 +00:00
func (w *FileLogWriter) Destroy() {
w.mw.fd.Close()
}
2016-11-26 11:53:29 +00:00
// Flush flush file logger.
2014-07-26 04:24:27 +00:00
// there are no buffering messages in file logger in memory.
// flush file means sync file from disk.
func (w *FileLogWriter) Flush() {
w.mw.fd.Sync()
}
func init() {
Register("file", NewFileWriter)
}