下面小編就為大家帶來一篇golang之log rotate詳解。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧
作業系統: CentOS 6.9_x64
go語言版本: 1.8.3
##問題描述
golang的log模組提供的有寫日誌功能,範例程式碼如下:/* golang log example */ package main import ( "log" "os" ) func main() { logFile,err := os.Create("test1.log") defer logFile.Close() if err != nil { log.Fatalln("open file error!") } logger := log.New(logFile,"[Debug]",log.Ldate | log.Ltime | log.Lshortfile) logger.Println("test debug message") logger.SetPrefix("[Info]") logger.Println("test info message") }執行效果:
[root@local t2]# go build logTest1.go [root@local t2]# ./logTest1 [root@local t2]# cat test1.log [Debug]2017/06/13 23:18:36 logTest1.go:19: test debug message [Info]2017/06/13 23:18:36 logTest1.go:21: test info message [root@local t2]#go語言的log模組沒有提供log rotate接口,但實際開發中我們需要該功能:我們不希望單一日誌過大,否則文字編輯器無法打開,查看比較困難;更不希望佔用太大的存儲空間,可以指定最多存多少個日誌檔。
解決方案
借助帶有緩衝的channel來實現。範例程式碼如下:
/* golang log rotate example */ package main import ( "fmt" "log" "os" "time" ) const ( BACKUP_COUNT = 5 MAX_FILE_BYTES = 2 * 1024 ) func doRotate(fPrefix string) { for j := BACKUP_COUNT; j >= 1; j-- { curFileName := fmt.Sprintf("%s_%d.log",fPrefix,j) k := j-1 preFileName := fmt.Sprintf("%s_%d.log",fPrefix,k) if k == 0 { preFileName = fmt.Sprintf("%s.log", fPrefix) } _,err := os.Stat(curFileName) if err == nil { os.Remove(curFileName) fmt.Println("remove : ", curFileName) } _,err = os.Stat(preFileName) if err == nil { fmt.Println("rename : ", preFileName, " => ", curFileName) err = os.Rename(preFileName, curFileName) if err != nil { fmt.Println(err) } } } } func NewLogger(fPrefix string) (*log.Logger, *os.File) { var logger *log.Logger fileName := fmt.Sprintf("%s.log", fPrefix) fmt.Println("fileName :", fileName) logFile, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { fmt.Println("open file error!") } else { logger = log.New(logFile, "[Debug]", log.Ldate|log.Ltime|log.Lshortfile) } return logger, logFile } func logWorker(msgQueue <-chan string) { fPrefix := "msg" logger, logFile := NewLogger(fPrefix) for msg := range msgQueue { logger.Println(msg) fi, err2 := logFile.Stat() if err2 == nil { if fi.Size() > MAX_FILE_BYTES { logFile.Close() doRotate(fPrefix) logger,logFile = NewLogger(fPrefix) } } } logFile.Close() } func main() { msgQueue := make(chan string, 1000) go logWorker(msgQueue) for j := 1; j <= 1000; j++ { msgQueue <- fmt.Sprintf("msg_%d", j) time.Sleep(1 * time.Second) } close(msgQueue) return }運行效果如下:
[root@local t2]# ./logRotateTest1 fileName : msg.log rename : msg.log => msg_1.log fileName : msg.log rename : msg_1.log => msg_2.log rename : msg.log => msg_1.log fileName : msg.log rename : msg_2.log => msg_3.log rename : msg_1.log => msg_2.log rename : msg.log => msg_1.log fileName : msg.log ^C
討論
以上是詳細介紹golang中的log模組的詳細內容。更多資訊請關注PHP中文網其他相關文章!