From 51b1095e73d46eb21d7bb8042285703e7038ec2b Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 3 Feb 2016 16:32:59 +0800 Subject: [PATCH] add files logger --- logs/file.go | 16 +++++++------ logs/files.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ logs/log.go | 4 +++- 3 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 logs/files.go diff --git a/logs/file.go b/logs/file.go index 4f228267..990a76d8 100644 --- a/logs/file.go +++ b/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 diff --git a/logs/files.go b/logs/files.go new file mode 100644 index 00000000..42528660 --- /dev/null +++ b/logs/files.go @@ -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) +} diff --git a/logs/log.go b/logs/log.go index f53d2382..e5c14dc1 100644 --- a/logs/log.go +++ b/logs/log.go @@ -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