This is a creation in Article, where the information may have evolved or changed.
Operating system: CentOS 6.9_x64
Go language version: 1.8.3
Problem description
The log module of Golang provides the write log function, the sample code is as follows:
/*Golang Log Example e-mail:mike_zhang@live.com*/Package main Import ("Log" "OS") Func main () {logfile,err:= OS. Create ("Test1.log") defer logfile.close ()ifErr! =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") }
Operating effect:
[root@local t2]# go Build logtest1.go [root@local t2]#./LogTest1 [root@local t2]#CatTest1.log [Debug] ./ ./ - at: -: $Logtest1.go: +: Test Debug message [Info] ./ ./ - at: -: $Logtest1.go: +: TestInfomessage [root@local t2]#
The log module for the Go language does not provide a log rotate interface, but in practice we need this feature:
We do not want a single log too large, otherwise the text editor can not open, viewing more difficult;
You do not want to consume too much storage space and you can specify the maximum number of log files to save.
Solution Solutions
This is achieved with a buffered channel.
The sample code is as follows:
/*Golang Log Rotate example e-mail:mike_zhang@live.com*/Package main Import ("FMT" "Log" "OS" " Time" ) Const(Backup_count=5max_file_bytes=2*1024x768) func dorotate (Fprefixstring) { forJ: = Backup_count; J >=1; j--{curfilename:= Fmt. Sprintf ("%s_%d.log", Fprefix,j) K:= J1Prefilename:= Fmt. Sprintf ("%s_%d.log", Fprefix,k)ifK = =0{prefilename= Fmt. Sprintf ("%s.log", Fprefix)} _,err:=OS. Stat (Curfilename)ifErr = =Nil {os. Remove (Curfilename) fmt. Println ("Remove:", Curfilename)} _,err=OS. Stat (Prefilename)ifErr = =Nil {fmt. Println ("Rename:", Prefilename,"=", curfilename) Err=OS. Rename (Prefilename, Curfilename)ifErr! =Nil {fmt. PRINTLN (Err)}}}} func Newlogger (Fprefixstring) (*log. Logger, *OS. File) {varLogger *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) ifErr! =Nil {fmt. Println ("Open File error!") } Else{Logger= log. New (LogFile,"[Debug]", log. Ldate|log. ltime|log. Lshortfile)}returnlogger, LogFile} func logworker (Msgqueue<-chanstring) {fprefix:="msg"logger, LogFile:=Newlogger (Fprefix) formsg: =range Msgqueue {logger. PRINTLN (msg) fi, ERR2:=Logfile.stat ()ifERR2 = =Nil {ifFi. Size () >max_file_bytes {logfile.close () dorotate (fprefix) logger,logfile=Newlogger (Fprefix)}} } logfile.close ()} func main () {msgqueue:= Make (chanstring, +) Go Logworker (msgqueue) forJ: =1; J <= +; J + +{msgqueue<-FMT. Sprintf ("msg_%d", J) time. Sleep (1*Time . Second)} close (Msgqueue)return }
The results are as follows:
[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
Discuss
Here is just a simple example code that implements the log rotate, and more functions need to be developed on their own.
All right, that's it, I hope it helps you.
This article GitHub address:
the Logrotate.rst of https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170614_golang
Welcome to Supplement