From 987151550397d8e4bbd9996791afe73517e8db0a Mon Sep 17 00:00:00 2001 From: Jay Zhang Date: Mon, 7 Oct 2019 17:38:35 +0800 Subject: [PATCH] feat: use glog instead of go log --- .gitignore | 1 + cmd/diagnosis/main.go | 10 ++++++---- diagnosis/fetching.go | 16 ++++++++-------- diagnosis/info.go | 5 +++-- go.mod | 5 ++++- go.sum | 2 ++ 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index cf9de60..5bd4d61 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.so *.dylib cmd/diagnosis/diagnosis +cmd/diagnosis/diagnosis.* # Test binary, build with `go test -c` *.test diff --git a/cmd/diagnosis/main.go b/cmd/diagnosis/main.go index 4d0dad2..fb24468 100644 --- a/cmd/diagnosis/main.go +++ b/cmd/diagnosis/main.go @@ -1,16 +1,17 @@ package main import ( + "flag" "fmt" - "log" "net/http" + "github.com/golang/glog" "github.com/wangyoucao577/go-project-layout/diagnosis" ) func main() { - //log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Llongfile) - log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile) + flag.Parse() // parse flags for glog + defer glog.Flush() mux := http.NewServeMux() @@ -29,5 +30,6 @@ func main() { fmt.Fprintf(w, "no diagnosis command %s", req.URL) }) - log.Fatal(http.ListenAndServe(":8000", mux)) + glog.Info("Listen on :8000") + glog.Fatal(http.ListenAndServe(":8000", mux)) } diff --git a/diagnosis/fetching.go b/diagnosis/fetching.go index b2763f1..7bee04b 100644 --- a/diagnosis/fetching.go +++ b/diagnosis/fetching.go @@ -1,10 +1,10 @@ package diagnosis import ( - "log" "net" "os" + "github.com/golang/glog" "github.com/matishsiao/goInfo" ) @@ -15,37 +15,37 @@ func New() *Info { //hostname hostname, err := os.Hostname() if err != nil { - log.Printf("get hostname failed, err %v\n", err) + glog.Warningf("get hostname failed, err %v\n", err) } else { - log.Printf("Hostname: %s\n", hostname) + glog.V(2).Infof("Hostname: %s\n", hostname) info.Hostname = hostname } //ip addresses addrs, err := net.InterfaceAddrs() if err != nil { - log.Printf("lookup network interface addrs failed, err %v\n", err) + glog.Warningf("lookup network interface addrs failed, err %v\n", err) } else { for _, addr := range addrs { ip, _, err := net.ParseCIDR(addr.String()) if err != nil { - log.Printf("ParseCIDR addrs failed, err %v\n", err) + glog.Warningf("ParseCIDR addrs failed, err %v\n", err) continue } if ip.IsLoopback() { - log.Printf("ignore Loopback ip address-->%s\n", addr.String()) + glog.V(2).Infof("ignore Loopback ip address-->%s\n", addr.String()) continue } - log.Printf("%s ip address-->%s\n", addr.Network(), addr.String()) + glog.V(2).Infof("%s ip address-->%s\n", addr.Network(), addr.String()) info.IPAddresses = append(info.IPAddresses, addr.String()) } } //from goInfo gi := goInfo.GetInfo() - log.Println(gi) + glog.V(2).Infoln(gi) info.CPUs = gi.CPUs return &info diff --git a/diagnosis/info.go b/diagnosis/info.go index 81c50ec..b0ea6b3 100644 --- a/diagnosis/info.go +++ b/diagnosis/info.go @@ -2,7 +2,8 @@ package diagnosis import ( "encoding/json" - "log" + + "github.com/golang/glog" ) // Info includes information for diagnosis. @@ -19,7 +20,7 @@ type Info struct { func (info Info) String() string { jsonstr, err := json.Marshal(info) if err != nil { - log.Fatalf("to json failed, err %v\n", err) + glog.Errorf("to json failed, err %v\n", err) return err.Error() } return string(jsonstr) diff --git a/go.mod b/go.mod index 8328705..0aea4a6 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/wangyoucao577/go-project-layout go 1.13 -require github.com/matishsiao/goInfo v0.0.0-20170803142006-617e6440957e +require ( + github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b + github.com/matishsiao/goInfo v0.0.0-20170803142006-617e6440957e +) diff --git a/go.sum b/go.sum index 01bfd9b..308f6b6 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/matishsiao/goInfo v0.0.0-20170803142006-617e6440957e h1:Y+GY+bv5vf1gssphFsGiq6R8qdHxnpDZvYljFnXfhD8= github.com/matishsiao/goInfo v0.0.0-20170803142006-617e6440957e/go.mod h1:yLZrFIhv+Z20hxHvcZpEyKVQp9HMsOJkXAxx7yDqtvg=