Finish the logger maintain things.

This commit is contained in:
zicla
2018-11-30 22:01:31 +08:00
parent 84703a8971
commit 1bdfb4996e
8 changed files with 138 additions and 15 deletions

View File

@ -4,6 +4,8 @@ import (
"fmt"
"log"
"os"
"sync"
"time"
)
//日志系统必须高保
@ -12,14 +14,24 @@ var LOGGER *Logger = &Logger{}
//在Logger的基础上包装一个全新的Logger.
type Logger struct {
//加锁,在维护日志期间,禁止写入日志。
sync.RWMutex
//继承logger
goLogger *log.Logger
//日志记录所在的文件
file *os.File
//每天凌晨定时整理器
maintainTimer *time.Timer
}
//处理日志的统一方法。
func (this *Logger) log(prefix string, format string, v ...interface{}) {
this.Lock()
defer this.Unlock()
//控制台中打印日志
fmt.Printf(format+"\r\n", v...)
this.goLogger.SetPrefix(prefix)
@ -28,32 +40,82 @@ func (this *Logger) log(prefix string, format string, v ...interface{}) {
//处理日志的统一方法。
func (this *Logger) Debug(format string, v ...interface{}) {
this.log("[debug]", format, v...)
this.log("[DEBUG]", format, v...)
}
func (this *Logger) Info(format string, v ...interface{}) {
this.log("[info ]", format, v...)
this.log("[INFO ]", format, v...)
}
func (this *Logger) Warn(format string, v ...interface{}) {
this.log("[warn ]", format, v...)
this.log("[WARN ]", format, v...)
}
func (this *Logger) Error(format string, v ...interface{}) {
this.log("[error]", format, v...)
this.log("[ERROR]", format, v...)
}
func (this *Logger) Panic(format string, v ...interface{}) {
this.log("[panic]", format, v...)
this.log("[PANIC]", format, v...)
panic(fmt.Sprintf(format, v...))
}
func (this *Logger) Init() {
this.openFile()
//日志需要自我备份,自我维护。明天第一秒触发
nextTime := FirstSecondOfDay(Tomorrow())
duration := nextTime.Sub(time.Now())
this.maintainTimer = time.AfterFunc(duration, func() {
go this.maintain()
})
}
//将日志写入到今天的日期中(该方法内必须使用异步方法记录日志,否则会引发死锁)
func (this *Logger) maintain() {
this.Lock()
defer this.Unlock()
go this.Info("每日维护日志")
//首先关闭文件。
this.closeFile()
//日志归类到昨天
destPath := GetLogPath() + "/tank-" + Yesterday().Local().Format("2006-01-02") + ".log"
//直接重命名文件
err := os.Rename(this.fileName(), destPath)
if err != nil {
go this.Error("重命名文件出错", err.Error())
}
//再次打开文件
this.openFile()
//准备好下次维护日志的时间。
now := time.Now()
nextTime := FirstSecondOfDay(Tomorrow())
duration := nextTime.Sub(now)
this.maintainTimer = time.AfterFunc(duration, func() {
go this.maintain()
})
}
//日志名称
func (this *Logger) fileName() string {
return GetLogPath() + "/tank.log"
}
//打开日志文件
func (this *Logger) openFile() {
//日志输出到文件中 文件打开后暂时不关闭
filePath := GetLogPath() + "/tank.log"
fmt.Printf("使用日志文件 %s\r\n", filePath)
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
fmt.Printf("使用日志文件 %s\r\n", this.fileName())
f, err := os.OpenFile(this.fileName(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic("日志文件无法正常打开: " + err.Error())
}
@ -62,7 +124,8 @@ func (this *Logger) Init() {
this.file = f
}
func (this *Logger) Destroy() {
//关闭日志文件
func (this *Logger) closeFile() {
if this.file != nil {
err := this.file.Close()
if err != nil {
@ -71,3 +134,13 @@ func (this *Logger) Destroy() {
}
}
func (this *Logger) Destroy() {
this.closeFile()
if this.maintainTimer != nil {
this.maintainTimer.Stop()
}
}