"encoding/json"
"errors"
"fmt"
- "io/ioutil"
"log"
"os"
"path/filepath"
// The opened file
Filename string `json:"filename"`
- Maxlines int `json:"maxlines"`
- maxlinesCurlines int
-
// Rotate at size
Maxsize int `json:"maxsize"`
maxsizeCursize int
func NewFileWriter() LoggerInterface {
w := &FileLogWriter{
Filename: "",
- Maxlines: 1000000,
Maxsize: 1 << 28, //256 MB
Daily: true,
Maxdays: 7,
// config like:
// {
// "filename":"log/gogs.log",
-// "maxlines":10000,
// "maxsize":1<<30,
// "daily":true,
// "maxdays":15,
func (w *FileLogWriter) docheck(size int) {
w.startLock.Lock()
defer w.startLock.Unlock()
- if w.Rotate && ((w.Maxlines > 0 && w.maxlinesCurlines >= w.Maxlines) ||
- (w.Maxsize > 0 && w.maxsizeCursize >= w.Maxsize) ||
+ if w.Rotate && ((w.Maxsize > 0 && w.maxsizeCursize >= w.Maxsize) ||
(w.Daily && time.Now().Day() != w.dailyOpenDate)) {
if err := w.DoRotate(); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
return
}
}
- w.maxlinesCurlines++
w.maxsizeCursize += size
}
}
w.maxsizeCursize = int(finfo.Size())
w.dailyOpenDate = time.Now().Day()
- if finfo.Size() > 0 {
- content, err := ioutil.ReadFile(w.Filename)
- if err != nil {
- return err
- }
- w.maxlinesCurlines = len(strings.Split(string(content), "\n"))
- } else {
- w.maxlinesCurlines = 0
- }
return nil
}
}
LogConfigs[i] = fmt.Sprintf(
- `{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level,
+ `{"level":%s,"filename":"%s","rotate":%v,"maxsize":%d,"daily":%v,"maxdays":%d}`, level,
logPath,
sec.Key("LOG_ROTATE").MustBool(true),
- sec.Key("MAX_LINES").MustInt(1000000),
1<<uint(sec.Key("MAX_SIZE_SHIFT").MustInt(28)),
sec.Key("DAILY_ROTATE").MustBool(true),
sec.Key("MAX_DAYS").MustInt(7))
logPath = path.Join(filepath.Dir(logPath), "xorm.log")
logConfigs = fmt.Sprintf(
- `{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level,
+ `{"level":%s,"filename":"%s","rotate":%v,"maxsize":%d,"daily":%v,"maxdays":%d}`, level,
logPath,
sec.Key("LOG_ROTATE").MustBool(true),
- sec.Key("MAX_LINES").MustInt(1000000),
1<<uint(sec.Key("MAX_SIZE_SHIFT").MustInt(28)),
sec.Key("DAILY_ROTATE").MustBool(true),
sec.Key("MAX_DAYS").MustInt(7))