This is a creation in Article, where the information may have evolved or changed.
Golang Program Log records
Package Mainimport ("Flag" "FMT" "Log" "OS" "Runtime") var (logfilename = flag.) String ("Log", "CServer.log", "Log file name")) Func main () {runtime. Gomaxprocs (runtime. NUMCPU ()) flag. Parse ()//set logfile stdoutlogfile, LogErr: = OS. OpenFile (*logfilename, OS. O_create|os. O_rdwr|os. O_append, 0666) if logerr! = nil {fmt. Println ("Fail to find", *logfile, "cserver start Failed") os. Exit (1)}log. Setoutput (logFile) log. SetFlags (log. Ldate | Log. Ltime | Log. Lshortfile)//write Loglog. Printf ("Server abort! CAUSE:%V \ n "," Test log File ")}
Description
First, after the main program starts to initialize the log format, later with LOG.XXX will be recorded in the original CServer.log file
Second, the above initialization format is shown in the log file as:
2013/03/19 10:44:26 Main.go:29:server abort! Cause:test log file 2013/03/19 10:44:27 main.go:29:server abort! Cause:test log File
Third, you can pass the log. SetFlags () Customize the format you want to express
Four, set the output destination log. Setoutput ()
Five, OS. OpenFile (*logfilename, OS. O_create|os. O_rdwr|os. O_append, 0666) This is the creation of the log file.
5.1 If the log file does not exist, create a new file os. O_create
5.2 Open the read-write OS for the file . O_rdwr
5.3 Writes log information to the log file, inheriting to the current log file, not overwriting the OS. O_append
5.3log file Permissions bit 0666(that is, all users can read and write)