1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 21:01:31 +00:00

add files logger

This commit is contained in:
JessonChan 2016-02-03 16:32:59 +08:00
parent 68cc53e92b
commit 51b1095e73
3 changed files with 78 additions and 8 deletions

View File

@ -53,6 +53,8 @@ type fileLogWriter struct {
Level int `json:"level"` Level int `json:"level"`
Perm os.FileMode `json:"perm"` Perm os.FileMode `json:"perm"`
fileNameOnly, suffix string // like "project.log", project is fileNameOnly and .log is suffix
} }
// newFileWriter create a FileLogWriter returning as LoggerInterface. // newFileWriter create a FileLogWriter returning as LoggerInterface.
@ -89,6 +91,11 @@ func (w *fileLogWriter) Init(jsonConfig string) error {
if len(w.Filename) == 0 { if len(w.Filename) == 0 {
return errors.New("jsonconfig must have filename") return errors.New("jsonconfig must have filename")
} }
w.suffix = filepath.Ext(w.Filename)
w.fileNameOnly = strings.TrimSuffix(w.Filename, w.suffix)
if w.suffix == "" {
w.suffix = ".log"
}
err = w.startLogger() err = w.startLogger()
return err return err
} }
@ -205,18 +212,13 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error {
// Find the next available number // Find the next available number
num := 1 num := 1
fName := "" fName := ""
suffix := filepath.Ext(w.Filename)
filenameOnly := strings.TrimSuffix(w.Filename, suffix)
if suffix == "" {
suffix = ".log"
}
if w.MaxLines > 0 || w.MaxSize > 0 { if w.MaxLines > 0 || w.MaxSize > 0 {
for ; err == nil && num <= 999; num++ { for ; err == nil && num <= 999; num++ {
fName = filenameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format("2006-01-02"), num, suffix) fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format("2006-01-02"), num, w.suffix)
_, err = os.Lstat(fName) _, err = os.Lstat(fName)
} }
} else { } else {
fName = fmt.Sprintf("%s.%s.%s", filenameOnly, logTime.Format("2006-01-02"), suffix) fName = fmt.Sprintf("%s.%s.%s", w.fileNameOnly, logTime.Format("2006-01-02"), w.suffix)
_, err = os.Lstat(fName) _, err = os.Lstat(fName)
} }
// return error if the last file checked still existed // return error if the last file checked still existed

66
logs/files.go Normal file
View File

@ -0,0 +1,66 @@
package logs
import "time"
type filesLogWriter struct {
writers [LevelDebug + 1]*fileLogWriter
}
func (f *filesLogWriter) Init(config string) error {
writer := newFileWriter().(*fileLogWriter)
err := writer.Init(config)
if err != nil {
return err
}
f.writers[0] = writer
for i := LevelEmergency; i <= f.writers[0].Level; i++ {
writer = newFileWriter().(*fileLogWriter)
writer.Init(config)
writer.Level = i
writer.fileNameOnly += "." + levelNames[i]
f.writers[i+1] = writer
}
return nil
}
func (f *filesLogWriter) Destroy() {
for i := 0; i < len(f.writers); i++ {
if f.writers[i] != nil {
f.writers[i].Destroy()
}
}
}
func (f *filesLogWriter) WriteMsg(when time.Time, msg string, level int) error {
if f.writers[0] != nil {
f.writers[0].WriteMsg(when, msg, level)
}
for i := 1; i < len(f.writers); i++ {
if f.writers[i] != nil {
if level == f.writers[i].Level {
f.writers[i].WriteMsg(when, msg, level)
}
}
}
return nil
}
func (f *filesLogWriter) Flush() {
for i := 0; i < len(f.writers); i++ {
if f.writers[i] != nil {
f.writers[i].Flush()
}
}
}
// newFilesWriter create a FileLogWriter returning as LoggerInterface.
func newFilesWriter() Logger {
return &filesLogWriter{}
}
func init() {
Register("files", NewConn)
}

View File

@ -64,6 +64,8 @@ const (
LevelWarn = LevelWarning LevelWarn = LevelWarning
) )
var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"}
type loggerType func() Logger type loggerType func() Logger
// Logger defines the behavior of a log provider. // Logger defines the behavior of a log provider.
@ -109,7 +111,7 @@ type nameLogger struct {
type logMsg struct { type logMsg struct {
level int level int
msg string msg string
when time.Time when time.Time
} }
var logMsgPool *sync.Pool var logMsgPool *sync.Pool