mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 14:30:56 +00:00
add files logger
This commit is contained in:
parent
68cc53e92b
commit
51b1095e73
16
logs/file.go
16
logs/file.go
@ -53,6 +53,8 @@ type fileLogWriter struct {
|
||||
Level int `json:"level"`
|
||||
|
||||
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.
|
||||
@ -89,6 +91,11 @@ func (w *fileLogWriter) Init(jsonConfig string) error {
|
||||
if len(w.Filename) == 0 {
|
||||
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()
|
||||
return err
|
||||
}
|
||||
@ -205,18 +212,13 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error {
|
||||
// Find the next available number
|
||||
num := 1
|
||||
fName := ""
|
||||
suffix := filepath.Ext(w.Filename)
|
||||
filenameOnly := strings.TrimSuffix(w.Filename, suffix)
|
||||
if suffix == "" {
|
||||
suffix = ".log"
|
||||
}
|
||||
if w.MaxLines > 0 || w.MaxSize > 0 {
|
||||
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)
|
||||
}
|
||||
} 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)
|
||||
}
|
||||
// return error if the last file checked still existed
|
||||
|
66
logs/files.go
Normal file
66
logs/files.go
Normal 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)
|
||||
}
|
@ -64,6 +64,8 @@ const (
|
||||
LevelWarn = LevelWarning
|
||||
)
|
||||
|
||||
var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"}
|
||||
|
||||
type loggerType func() Logger
|
||||
|
||||
// Logger defines the behavior of a log provider.
|
||||
@ -109,7 +111,7 @@ type nameLogger struct {
|
||||
type logMsg struct {
|
||||
level int
|
||||
msg string
|
||||
when time.Time
|
||||
when time.Time
|
||||
}
|
||||
|
||||
var logMsgPool *sync.Pool
|
||||
|
Loading…
Reference in New Issue
Block a user