From 08fb0841178edd92ed462f4cb5272d2704f495fd Mon Sep 17 00:00:00 2001 From: gordberg Date: Wed, 18 May 2016 15:19:58 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix(E05161CM):app=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=8D=A2=E6=88=90=E6=BB=9A=E5=8A=A8=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- printer.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 2 deletions(-) diff --git a/printer.go b/printer.go index 244e807..f363825 100644 --- a/printer.go +++ b/printer.go @@ -1,13 +1,34 @@ package log import ( + "fmt" "io" "os" + "path/filepath" + "strings" + "time" + + "github.com/robfig/cron" +) + +const ( + QuickPay = "quickpay" + Phantom = "phantom" + LogDir = "logs/" + FileType = ".log" ) +var LogFile *os.File = nil +var FileName string = "" + func init() { - // 默认实现标准格式标准输出 - SetPrinter(NewStandard(os.Stdout, DefaultFormat)) + // 日志初始化 + PreLog() + RemoveLogFile() + c := cron.New() + c.AddFunc("0 0 0 * * *", PreLog) // 每隔一天 + c.AddFunc("0 0 0 1 */2 *", RemoveLogFile) // 每隔2个月 + c.Start() } // Printer 定义了打印接口 @@ -22,3 +43,101 @@ type Printer interface { // ChangeWriter 改变输出流 ChangeWriter(w io.Writer) } + +// 由于改方法是定时任务,第一次和程序一起启动,如果失败,在控制台输出错误信息,如果后续定时任务出现处理失败,则沿用原来的log文件 +func PreLog() { + if FileName == "" { + sPath, err := os.Getwd() + if err != nil { + slog := fmt.Sprintf("get the current file path error, the error is %s\n", err) + if LogFile == nil { + fmt.Println(slog) + os.Exit(1) + } + Error(slog) + } + strArrays := strings.Split(sPath, "/") + ilen := len(strArrays) + if ilen == 0 { + slog := fmt.Sprintf("parse the file path to array error, the filepath is %s\n", sPath) + if LogFile == nil { + fmt.Println(slog) + os.Exit(1) + } + Error(slog) + } + FileName = strArrays[ilen-1] + } + + if (FileName != QuickPay) && (FileName != Phantom) { + SetPrinter(NewStandard(os.Stdout, DefaultFormat)) + return + } + + // 类似phantom20160512.log与quickpay20160512.log + sName := LogDir + FileName + time.Now().Format("20060102") + FileType + pFile, err := os.Create(sName) + if (err != nil) || (pFile == nil) { + slog := fmt.Sprintf("create the log file error, the error is %s\n", err) + if LogFile == nil { + fmt.Println(slog) + os.Exit(1) + } + Error(slog) + } + + if LogFile != nil { + err = LogFile.Close() + if err != nil { + Errorf("close the log file %s fail, error is %s", LogFile.Name(), err) + } + } + LogFile = pFile + SetPrinter(NewStandard(LogFile, DefaultFormat)) +} + +func RemoveLogFile() { + sPath, err := os.Getwd() + if err != nil { + Errorf("get the current file path error, the error is %s\n", err) + return + } + sPath += "/" + LogDir + fullPath, err := filepath.Abs(sPath) + if err != nil { + Errorf("get the absolute path error, the error is %s, the path is %s", err, fullPath) + return + } + // 一天前 + d, err := time.ParseDuration("-24h") + if err != nil { + Errorf("parse the time error, the error is %s", err) + return + } + cur := time.Now() + curDate := cur.Add(d * 60).Format("20060102") + + filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error { + if !info.IsDir() && strings.Index(info.Name(), FileType) > 0 { + nameArray := []byte(info.Name()) + var date []byte + date = nameArray[len(FileName):] + sArray := strings.Split(string(date), ".") + if IsMoreTwoMonth(sArray[0], curDate) { // 超过两个月删除 + err = os.Remove(sPath + info.Name()) + if err != nil { + Errorf("delete the file %s error, error is ", info.Name(), err) + return err + } + } + } + return nil + }) +} + +func IsMoreTwoMonth(origDate, curDate string) bool { + if strings.Compare(origDate, curDate) <= 0 { + return true + } + return false +} From 377d7347fde86740ebd4a4ad58a697762bf593a3 Mon Sep 17 00:00:00 2001 From: gordberg Date: Wed, 18 May 2016 17:21:19 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- printer.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/printer.go b/printer.go index f363825..9352544 100644 --- a/printer.go +++ b/printer.go @@ -12,10 +12,11 @@ import ( ) const ( - QuickPay = "quickpay" - Phantom = "phantom" - LogDir = "logs/" - FileType = ".log" + QuickPay = "quickpay" + Phantom = "phantom" + AngryCard = "angrycard" + LogDir = "logs/" + FileType = ".log" ) var LogFile *os.File = nil @@ -69,13 +70,13 @@ func PreLog() { FileName = strArrays[ilen-1] } - if (FileName != QuickPay) && (FileName != Phantom) { + if (FileName != QuickPay) && (FileName != Phantom) && (FileName != AngryCard) { SetPrinter(NewStandard(os.Stdout, DefaultFormat)) return } - // 类似phantom20160512.log与quickpay20160512.log - sName := LogDir + FileName + time.Now().Format("20060102") + FileType + // 类似phantom_20160512.log与quickpay_20160512.log + sName := LogDir + FileName + "_" + time.Now().Format("20060102") + FileType pFile, err := os.Create(sName) if (err != nil) || (pFile == nil) { slog := fmt.Sprintf("create the log file error, the error is %s\n", err) @@ -121,7 +122,7 @@ func RemoveLogFile() { if !info.IsDir() && strings.Index(info.Name(), FileType) > 0 { nameArray := []byte(info.Name()) var date []byte - date = nameArray[len(FileName):] + date = nameArray[len(FileName)+1:] sArray := strings.Split(string(date), ".") if IsMoreTwoMonth(sArray[0], curDate) { // 超过两个月删除 err = os.Remove(sPath + info.Name()) From 569992f6ea4e4665feded584f83abbaa8f2b719e Mon Sep 17 00:00:00 2001 From: gordberg Date: Wed, 18 May 2016 18:15:21 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix(E05162CM):app=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=9A=84=E5=AE=9A=E6=9C=9F=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- printer.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/printer.go b/printer.go index 9352544..98f530b 100644 --- a/printer.go +++ b/printer.go @@ -27,8 +27,8 @@ func init() { PreLog() RemoveLogFile() c := cron.New() - c.AddFunc("0 0 0 * * *", PreLog) // 每隔一天 - c.AddFunc("0 0 0 1 */2 *", RemoveLogFile) // 每隔2个月 + c.AddFunc("0 0 0 * * *", PreLog) // 每隔一天 + c.AddFunc("0 0 0 1 * *", RemoveLogFile) // 每隔一个月执行 c.Start() } @@ -56,6 +56,7 @@ func PreLog() { os.Exit(1) } Error(slog) + return } strArrays := strings.Split(sPath, "/") ilen := len(strArrays) @@ -66,6 +67,7 @@ func PreLog() { os.Exit(1) } Error(slog) + return } FileName = strArrays[ilen-1] } @@ -85,8 +87,10 @@ func PreLog() { os.Exit(1) } Error(slog) + return } + SetPrinter(NewStandard(pFile, DefaultFormat)) if LogFile != nil { err = LogFile.Close() if err != nil { @@ -94,7 +98,6 @@ func PreLog() { } } LogFile = pFile - SetPrinter(NewStandard(LogFile, DefaultFormat)) } func RemoveLogFile() { From 44143daa0520698cd70dd7f73d30f219bfdd7736 Mon Sep 17 00:00:00 2001 From: gordberg Date: Mon, 23 May 2016 13:43:53 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- printer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/printer.go b/printer.go index 98f530b..e58777f 100644 --- a/printer.go +++ b/printer.go @@ -122,7 +122,7 @@ func RemoveLogFile() { curDate := cur.Add(d * 60).Format("20060102") filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error { - if !info.IsDir() && strings.Index(info.Name(), FileType) > 0 { + if (info != nil) && (!info.IsDir()) && (strings.Index(info.Name(), FileType) > 0) { nameArray := []byte(info.Name()) var date []byte date = nameArray[len(FileName)+1:]