A log library for golang based on hashicorp/logutils
Yes, it another logging library for Go program language. Why do I create a new logging library for Go when there are so many popular logging library. such as:
For my daily use, i found several points are important for logging:
- level for logging shoud be easy to use
- content for logging should be friendly for human reading
- logs should be easy to check
so i create log4go
-
Step 01: get the library
$ go get -u github.com/liuliqiang/log4go -
Step 02: try in code:
func main() { log4go.Debug("I am debug log") log4go.Info("Web server is started at %s:%d", "127.0.0.1", 80) log4go.Warn("Get an empty http request") log4go.Error("Failed to query record from db: %v", errors.New("db error")) } -
Step 03: Run it!
$ go run main.go 2019/08/10 00:02:18 [INFO]Web server is started at 127.0.0.1:80 2019/08/10 00:02:18 [WARN]Get an empty http request 2019/08/10 00:02:18 [EROR]Failed to query record from db: db error
Just add a endpoint(such as http/grpc/...) to change log level such as:
http.HandleFunc("/logs", func(resp http.ResponseWriter, req *http.Request) {
switch req.URL.Query()["level"][0] {
case "debug":
log4go.SetLevel(log4go.LogLevelDebug)
case "info":
log4go.SetLevel(log4go.LogLevelInfo)
case "warning":
log4go.SetLevel(log4go.LogLevelWarn)
case "error":
log4go.SetLevel(log4go.LogLevelError)
}
return
})
- To be continue...
- More examples at Examples