frp/vendor/github.com/astaxie/beego/logs/file.go

283 lines
6.6 KiB
Go
Raw Normal View History

2016-01-27 21:24:36 +08:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logs
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
2016-08-11 16:32:05 +08:00
// fileLogWriter implements LoggerInterface.
2016-01-27 21:24:36 +08:00
// It writes messages by lines limit, file size limit, or time frequency.
2016-08-11 16:32:05 +08:00
type fileLogWriter struct {
sync.Mutex // write log order by order and atomic incr maxLinesCurLines and maxSizeCurSize
2016-01-27 21:24:36 +08:00
// The opened file
2016-08-11 16:32:05 +08:00
Filename string `json:"filename"`
fileWriter *os.File
2016-01-27 21:24:36 +08:00
2016-08-11 16:32:05 +08:00
// Rotate at line
MaxLines int `json:"maxlines"`
maxLinesCurLines int
2016-01-27 21:24:36 +08:00
// Rotate at size
2016-08-11 16:32:05 +08:00
MaxSize int `json:"maxsize"`
maxSizeCurSize int
2016-01-27 21:24:36 +08:00
// Rotate daily
2016-08-11 16:32:05 +08:00
Daily bool `json:"daily"`
MaxDays int64 `json:"maxdays"`
dailyOpenDate int
2016-01-27 21:24:36 +08:00
Rotate bool `json:"rotate"`
Level int `json:"level"`
2016-08-11 16:32:05 +08:00
Perm os.FileMode `json:"perm"`
2016-01-27 21:24:36 +08:00
2016-08-11 16:32:05 +08:00
fileNameOnly, suffix string // like "project.log", project is fileNameOnly and .log is suffix
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
// newFileWriter create a FileLogWriter returning as LoggerInterface.
func newFileWriter() Logger {
w := &fileLogWriter{
2016-01-27 21:24:36 +08:00
Filename: "",
2016-08-11 16:32:05 +08:00
MaxLines: 1000000,
MaxSize: 1 << 28, //256 MB
2016-01-27 21:24:36 +08:00
Daily: true,
2016-08-11 16:32:05 +08:00
MaxDays: 7,
2016-01-27 21:24:36 +08:00
Rotate: true,
Level: LevelTrace,
2016-08-11 16:32:05 +08:00
Perm: 0660,
2016-01-27 21:24:36 +08:00
}
return w
}
// Init file logger with json config.
2016-08-11 16:32:05 +08:00
// jsonConfig like:
2016-01-27 21:24:36 +08:00
// {
// "filename":"logs/beego.log",
2016-08-11 16:32:05 +08:00
// "maxLines":10000,
2016-01-27 21:24:36 +08:00
// "maxsize":1<<30,
// "daily":true,
2016-08-11 16:32:05 +08:00
// "maxDays":15,
// "rotate":true,
// "perm":0600
2016-01-27 21:24:36 +08:00
// }
2016-08-11 16:32:05 +08:00
func (w *fileLogWriter) Init(jsonConfig string) error {
err := json.Unmarshal([]byte(jsonConfig), w)
2016-01-27 21:24:36 +08:00
if err != nil {
return err
}
if len(w.Filename) == 0 {
return errors.New("jsonconfig must have filename")
}
2016-08-11 16:32:05 +08:00
w.suffix = filepath.Ext(w.Filename)
w.fileNameOnly = strings.TrimSuffix(w.Filename, w.suffix)
if w.suffix == "" {
w.suffix = ".log"
}
2016-01-27 21:24:36 +08:00
err = w.startLogger()
return err
}
// start file logger. create log file and set to locker-inside file writer.
2016-08-11 16:32:05 +08:00
func (w *fileLogWriter) startLogger() error {
file, err := w.createLogFile()
2016-01-27 21:24:36 +08:00
if err != nil {
return err
}
2016-08-11 16:32:05 +08:00
if w.fileWriter != nil {
w.fileWriter.Close()
}
w.fileWriter = file
2016-01-27 21:24:36 +08:00
return w.initFd()
}
2016-08-11 16:32:05 +08:00
func (w *fileLogWriter) needRotate(size int, day int) bool {
return (w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) ||
(w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) ||
(w.Daily && day != w.dailyOpenDate)
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
// WriteMsg write logger message into file.
func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
2016-01-27 21:24:36 +08:00
if level > w.Level {
return nil
}
2016-08-11 16:32:05 +08:00
h, d := formatTimeHeader(when)
msg = string(h) + msg + "\n"
if w.Rotate {
if w.needRotate(len(msg), d) {
w.Lock()
if w.needRotate(len(msg), d) {
if err := w.doRotate(when); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
}
}
w.Unlock()
}
}
w.Lock()
_, err := w.fileWriter.Write([]byte(msg))
if err == nil {
w.maxLinesCurLines++
w.maxSizeCurSize += len(msg)
}
w.Unlock()
return err
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
func (w *fileLogWriter) createLogFile() (*os.File, error) {
2016-01-27 21:24:36 +08:00
// Open the log file
2016-08-11 16:32:05 +08:00
fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, w.Perm)
2016-01-27 21:24:36 +08:00
return fd, err
}
2016-08-11 16:32:05 +08:00
func (w *fileLogWriter) initFd() error {
fd := w.fileWriter
fInfo, err := fd.Stat()
2016-01-27 21:24:36 +08:00
if err != nil {
return fmt.Errorf("get stat err: %s\n", err)
}
2016-08-11 16:32:05 +08:00
w.maxSizeCurSize = int(fInfo.Size())
w.dailyOpenDate = time.Now().Day()
w.maxLinesCurLines = 0
if fInfo.Size() > 0 {
2016-01-27 21:24:36 +08:00
count, err := w.lines()
if err != nil {
return err
}
2016-08-11 16:32:05 +08:00
w.maxLinesCurLines = count
2016-01-27 21:24:36 +08:00
}
return nil
}
2016-08-11 16:32:05 +08:00
func (w *fileLogWriter) lines() (int, error) {
2016-01-27 21:24:36 +08:00
fd, err := os.Open(w.Filename)
if err != nil {
return 0, err
}
defer fd.Close()
buf := make([]byte, 32768) // 32k
count := 0
lineSep := []byte{'\n'}
for {
c, err := fd.Read(buf)
if err != nil && err != io.EOF {
return count, err
}
count += bytes.Count(buf[:c], lineSep)
if err == io.EOF {
break
}
}
return count, nil
}
// DoRotate means it need to write file in new file.
2016-08-11 16:32:05 +08:00
// new file name like xx.2013-01-01.log (daily) or xx.001.log (by line or size)
func (w *fileLogWriter) doRotate(logTime time.Time) error {
2016-01-27 21:24:36 +08:00
_, err := os.Lstat(w.Filename)
2016-08-11 16:32:05 +08:00
if err != nil {
return err
}
// file exists
// Find the next available number
num := 1
fName := ""
if w.MaxLines > 0 || w.MaxSize > 0 {
2016-01-27 21:24:36 +08:00
for ; err == nil && num <= 999; num++ {
2016-08-11 16:32:05 +08:00
fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format("2006-01-02"), num, w.suffix)
_, err = os.Lstat(fName)
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
} else {
fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, logTime.Format("2006-01-02"), w.suffix)
_, err = os.Lstat(fName)
}
// return error if the last file checked still existed
if err == nil {
return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.Filename)
}
2016-01-27 21:24:36 +08:00
2016-08-11 16:32:05 +08:00
// close fileWriter before rename
w.fileWriter.Close()
2016-01-27 21:24:36 +08:00
2016-08-11 16:32:05 +08:00
// Rename the file to its new found name
// even if occurs error,we MUST guarantee to restart new logger
renameErr := os.Rename(w.Filename, fName)
// re-start logger
startLoggerErr := w.startLogger()
go w.deleteOldLog()
2016-01-27 21:24:36 +08:00
2016-08-11 16:32:05 +08:00
if startLoggerErr != nil {
return fmt.Errorf("Rotate StartLogger: %s\n", startLoggerErr)
}
if renameErr != nil {
return fmt.Errorf("Rotate: %s\n", renameErr)
2016-01-27 21:24:36 +08:00
}
return nil
2016-08-11 16:32:05 +08:00
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
func (w *fileLogWriter) deleteOldLog() {
2016-01-27 21:24:36 +08:00
dir := filepath.Dir(w.Filename)
filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
defer func() {
if r := recover(); r != nil {
2016-08-11 16:32:05 +08:00
fmt.Fprintf(os.Stderr, "Unable to delete old log '%s', error: %v\n", path, r)
2016-01-27 21:24:36 +08:00
}
}()
2016-08-11 16:32:05 +08:00
if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.MaxDays) {
if strings.HasPrefix(filepath.Base(path), w.fileNameOnly) &&
strings.HasSuffix(filepath.Base(path), w.suffix) {
2016-01-27 21:24:36 +08:00
os.Remove(path)
}
}
return
})
}
2016-08-11 16:32:05 +08:00
// Destroy close the file description, close file writer.
func (w *fileLogWriter) Destroy() {
w.fileWriter.Close()
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
// Flush flush file logger.
2016-01-27 21:24:36 +08:00
// there are no buffering messages in file logger in memory.
// flush file means sync file from disk.
2016-08-11 16:32:05 +08:00
func (w *fileLogWriter) Flush() {
w.fileWriter.Sync()
2016-01-27 21:24:36 +08:00
}
func init() {
2016-08-11 16:32:05 +08:00
Register("file", newFileWriter)
2016-01-27 21:24:36 +08:00
}