This is a creation in Article, where the information may have evolved or changed.
Today, we introduce a third-party package that is commonly used in the go language, that is, log4go. In C + +, there are log4cpp, and in Java, there are log4j.
Well, don't say much nonsense, now to introduce the use of Log4go. The links on Log4go on GitHub are as follows
Links: Https://github.com/skoo87/log4go
You can refer to the examples folder for specific usage , and the configuration file is in JSON format. A typical example is the following
Log.json:
{ "LogLevel": "Info", "FileWriter": { "LogPath": "Log-%y%m%d.log", "on": True }, " Consolewriter ": { " on ": True }}
Main.go:
Package Mainimport (log "Github.com/skoo87/log4go") func main () {if err: = log. Setuplogwithconf (".. /src/conf/log.json "); Err! = Nil {panic (err)}defer log. Close () var name = "Skoo" log. Debug ("Log4go by%s", name) log. Info ("Log4go by%s", name) log. Warn ("Log4go by%s", name) log. Error ("Log4go by%s", name) log. Fatal ("Log4go by%s", name)}
The new project is structured as follows
Compile in src directory, as follows
Then in the project root directory Log4go get the Bin folder, which has an executable file, execute this file, as follows
At the same time also got the log output file, the following structure
Well, if you want to refer to log4go more, the examples folder has a lot, the path is as follows
Examples path:log4go/src/github.com/skoo87/log4go/examples