2012-12-15 15:53:19 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
2013-06-24 15:24:18 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2012-12-15 15:53:19 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2013-06-24 15:24:18 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2012-12-15 15:53:19 +00:00
|
|
|
)
|
|
|
|
|
2013-06-24 15:24:18 +00:00
|
|
|
type FileLogWriter struct {
|
|
|
|
*log.Logger
|
2013-06-27 14:04:01 +00:00
|
|
|
mw *MuxWriter
|
2013-06-24 15:24:18 +00:00
|
|
|
// The opened file
|
|
|
|
filename string
|
|
|
|
|
|
|
|
maxlines int
|
|
|
|
maxlines_curlines int
|
|
|
|
|
|
|
|
// Rotate at size
|
|
|
|
maxsize int
|
|
|
|
maxsize_cursize int
|
|
|
|
|
|
|
|
// Rotate daily
|
|
|
|
daily bool
|
2013-06-25 10:54:51 +00:00
|
|
|
maxdays int64
|
2013-06-24 15:24:18 +00:00
|
|
|
daily_opendate int
|
|
|
|
|
|
|
|
rotate bool
|
|
|
|
|
2013-07-25 11:04:26 +00:00
|
|
|
startLock sync.Mutex // Only one log can write to the file
|
2013-06-24 15:24:18 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 14:04:01 +00:00
|
|
|
type MuxWriter struct {
|
|
|
|
sync.Mutex
|
|
|
|
fd *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *MuxWriter) Write(b []byte) (int, error) {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
return l.fd.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *MuxWriter) SetFd(fd *os.File) {
|
|
|
|
if l.fd != nil {
|
|
|
|
l.fd.Close()
|
|
|
|
}
|
|
|
|
l.fd = fd
|
|
|
|
}
|
|
|
|
|
2013-06-24 15:24:18 +00:00
|
|
|
func NewFileWriter(fname string, rotate bool) *FileLogWriter {
|
|
|
|
w := &FileLogWriter{
|
|
|
|
filename: fname,
|
|
|
|
maxlines: 1000000,
|
|
|
|
maxsize: 1 << 28, //256 MB
|
|
|
|
daily: true,
|
2013-06-25 10:54:51 +00:00
|
|
|
maxdays: 7,
|
2013-06-24 15:24:18 +00:00
|
|
|
rotate: rotate,
|
|
|
|
}
|
2013-06-27 14:04:01 +00:00
|
|
|
// 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)
|
2013-06-24 15:24:18 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2013-06-25 00:13:06 +00:00
|
|
|
// Set rotate at linecount (chainable). Must be called before call StartLogger
|
|
|
|
func (w *FileLogWriter) SetRotateLines(maxlines int) *FileLogWriter {
|
|
|
|
w.maxlines = maxlines
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set rotate at size (chainable). Must be called before call StartLogger
|
|
|
|
func (w *FileLogWriter) SetRotateSize(maxsize int) *FileLogWriter {
|
|
|
|
w.maxsize = maxsize
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set rotate daily (chainable). Must be called before call StartLogger
|
|
|
|
func (w *FileLogWriter) SetRotateDaily(daily bool) *FileLogWriter {
|
|
|
|
w.daily = daily
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2013-06-25 10:54:51 +00:00
|
|
|
// Set rotate daily's log keep for maxdays, other will delete
|
|
|
|
func (w *FileLogWriter) SetRotateMaxDays(maxdays int64) *FileLogWriter {
|
|
|
|
w.maxdays = maxdays
|
2013-06-25 00:13:06 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2013-06-24 15:24:18 +00:00
|
|
|
func (w *FileLogWriter) StartLogger() error {
|
2013-06-27 14:04:01 +00:00
|
|
|
fd, err := w.createLogFile()
|
|
|
|
if err != nil {
|
2013-06-24 15:24:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-06-27 14:04:01 +00:00
|
|
|
w.mw.SetFd(fd)
|
|
|
|
err = w.initFd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
BeeLogger = w
|
2013-06-24 15:24:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *FileLogWriter) docheck(size int) {
|
|
|
|
w.startLock.Lock()
|
|
|
|
defer w.startLock.Unlock()
|
|
|
|
if (w.maxlines > 0 && w.maxlines_curlines >= w.maxlines) ||
|
|
|
|
(w.maxsize > 0 && w.maxsize_cursize >= w.maxsize) ||
|
|
|
|
(w.daily && time.Now().Day() != w.daily_opendate) {
|
2013-06-27 14:04:01 +00:00
|
|
|
if err := w.DoRotate(); err != nil {
|
2013-06-24 15:24:18 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.filename, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.maxlines_curlines++
|
|
|
|
w.maxsize_cursize += size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *FileLogWriter) Printf(format string, v ...interface{}) {
|
|
|
|
// Perform the write
|
|
|
|
str := fmt.Sprintf(format, v...)
|
|
|
|
n := 24 + len(str) // 24 stand for the length "2013/06/23 21:00:22 [T] "
|
|
|
|
|
|
|
|
w.docheck(n)
|
|
|
|
w.Logger.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2013-06-27 14:04:01 +00:00
|
|
|
func (w *FileLogWriter) createLogFile() (*os.File, error) {
|
2013-06-24 15:24:18 +00:00
|
|
|
// Open the log file
|
|
|
|
fd, err := os.OpenFile(w.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
|
2013-06-27 14:04:01 +00:00
|
|
|
return fd, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *FileLogWriter) initFd() error {
|
|
|
|
fd := w.mw.fd
|
2013-06-24 15:24:18 +00:00
|
|
|
finfo, err := fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get stat err: %s\n", err)
|
|
|
|
}
|
|
|
|
w.maxsize_cursize = int(finfo.Size())
|
|
|
|
w.daily_opendate = time.Now().Day()
|
|
|
|
if finfo.Size() > 0 {
|
2013-06-25 03:19:03 +00:00
|
|
|
content, err := ioutil.ReadFile(w.filename)
|
2013-06-24 15:24:18 +00:00
|
|
|
if err != nil {
|
2013-06-25 03:19:03 +00:00
|
|
|
fmt.Println(err)
|
2013-06-24 15:24:18 +00:00
|
|
|
}
|
|
|
|
w.maxlines_curlines = len(strings.Split(string(content), "\n"))
|
|
|
|
} else {
|
|
|
|
w.maxlines_curlines = 0
|
|
|
|
}
|
2013-06-27 14:04:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
err = os.Rename(w.filename, fname)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Rotate: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-start logger
|
|
|
|
err = w.StartLogger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Rotate StartLogger: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go w.deleteOldLog()
|
|
|
|
}
|
|
|
|
|
2013-06-24 15:24:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *FileLogWriter) deleteOldLog() {
|
|
|
|
dir := path.Dir(w.filename)
|
|
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
2013-06-25 10:54:51 +00:00
|
|
|
if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.maxdays) {
|
2013-06-29 06:39:02 +00:00
|
|
|
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.filename)) {
|
|
|
|
os.Remove(path)
|
|
|
|
}
|
2013-06-24 15:24:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2012-12-15 15:53:19 +00:00
|
|
|
|
|
|
|
// Log levels to control the logging output.
|
|
|
|
const (
|
|
|
|
LevelTrace = iota
|
|
|
|
LevelDebug
|
|
|
|
LevelInfo
|
|
|
|
LevelWarning
|
|
|
|
LevelError
|
|
|
|
LevelCritical
|
|
|
|
)
|
|
|
|
|
|
|
|
// logLevel controls the global log level used by the logger.
|
|
|
|
var level = LevelTrace
|
|
|
|
|
|
|
|
// LogLevel returns the global log level and can be used in
|
|
|
|
// own implementations of the logger interface.
|
|
|
|
func Level() int {
|
|
|
|
return level
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogLevel sets the global log level used by the simple
|
|
|
|
// logger.
|
|
|
|
func SetLevel(l int) {
|
|
|
|
level = l
|
|
|
|
}
|
|
|
|
|
2013-06-24 15:24:18 +00:00
|
|
|
type IBeeLogger interface {
|
|
|
|
Fatal(v ...interface{})
|
|
|
|
Fatalf(format string, v ...interface{})
|
|
|
|
Fatalln(v ...interface{})
|
|
|
|
Flags() int
|
|
|
|
Output(calldepth int, s string) error
|
|
|
|
Panic(v ...interface{})
|
|
|
|
Panicf(format string, v ...interface{})
|
|
|
|
Panicln(v ...interface{})
|
|
|
|
Prefix() string
|
|
|
|
Print(v ...interface{})
|
|
|
|
Printf(format string, v ...interface{})
|
|
|
|
Println(v ...interface{})
|
|
|
|
SetFlags(flag int)
|
|
|
|
SetPrefix(prefix string)
|
|
|
|
}
|
|
|
|
|
2012-12-15 15:53:19 +00:00
|
|
|
// logger references the used application logger.
|
2013-06-24 15:24:18 +00:00
|
|
|
var BeeLogger IBeeLogger
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
BeeLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
|
|
|
}
|
2012-12-15 15:53:19 +00:00
|
|
|
|
|
|
|
// SetLogger sets a new logger.
|
|
|
|
func SetLogger(l *log.Logger) {
|
|
|
|
BeeLogger = l
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trace logs a message at trace level.
|
|
|
|
func Trace(v ...interface{}) {
|
|
|
|
if level <= LevelTrace {
|
|
|
|
BeeLogger.Printf("[T] %v\n", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug logs a message at debug level.
|
|
|
|
func Debug(v ...interface{}) {
|
|
|
|
if level <= LevelDebug {
|
|
|
|
BeeLogger.Printf("[D] %v\n", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info logs a message at info level.
|
|
|
|
func Info(v ...interface{}) {
|
|
|
|
if level <= LevelInfo {
|
|
|
|
BeeLogger.Printf("[I] %v\n", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warning logs a message at warning level.
|
|
|
|
func Warn(v ...interface{}) {
|
|
|
|
if level <= LevelWarning {
|
|
|
|
BeeLogger.Printf("[W] %v\n", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error logs a message at error level.
|
|
|
|
func Error(v ...interface{}) {
|
|
|
|
if level <= LevelError {
|
|
|
|
BeeLogger.Printf("[E] %v\n", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Critical logs a message at critical level.
|
|
|
|
func Critical(v ...interface{}) {
|
|
|
|
if level <= LevelCritical {
|
|
|
|
BeeLogger.Printf("[C] %v\n", v)
|
|
|
|
}
|
|
|
|
}
|