The article introduces 3 commonly used ways to activate logs and has annotated them
Write down the precautions first. It is recommended to test the three methods separately, because the first type of Fatal will terminate the run. The difference between Fatal and Panic and Print is that Fatal will save the log and terminate the program. Panic will save the log and throw out the abnormal termination program. Print will save the log but the program continues.
package main import ( "log" "bytes" "fmt" "os" "/astaxie/beego/logs" ) func main() { //1. The log is written into a cache var buf logger := (&buf, "logger: ", )//The three parameters correspond to the output object, prefix, file name, and then integrate into prefix ("Hello, log file!") //Actual log information (&buf) //2. Write the log into a file. After the refresh project is executed, the file will be opened to see fileName := "" //In the project path, you can also write absolute paths, but pay attention to escape characters logFile,err := (fileName) //Create this file and return the handle defer () //Make sure the file is closed after the function is executed if err != nil { ("open file error !") } //The log package in the library does not exist at the level, there are only three types: Print, Fatal, and Panic. If you want 7 levels, you can use syslog package or framework debugLog := (logFile,"",) ("Find a low-level bug, low-level log report, and continue to execute the debug record") ("A serious bug was found, the log was logged, the program was terminated here") ("Oh, I'm not executed, it's so miserable") //3. Use the log package of beego framework for log processing, corresponding to 7 levels logInstance:=() ("this is a message of http") //an official with prefix ORM ("ORM").Println("this is a message of orm") ("my book is bought in the year of ", 2016) ("this %s cat is %v years old", "yellow", 3) ("json is a type of kv like", map[string]int{"key": 2016}) (1024, "is a very", "good game") ("oh,crash") ("fuck") ("alert") ("emergency") }
The second type
package main import ( "os" "log" ) func main() { file, err := ("./testLog/", os.O_CREATE | os.O_WRONLY | os.O_APPEND,) if err != nil { (err) } logger := (file, "", |) ("Log 1.") ("Log 23") }
Summarize
This is the end of this article about the commonly used logging methods in Go. For more detailed explanations of Go language logs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!