Skip to content

Commit

Permalink
feat: use glog instead of go log
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyoucao577 committed Oct 7, 2019
1 parent 389796c commit 9871515
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.so
*.dylib
cmd/diagnosis/diagnosis
cmd/diagnosis/diagnosis.*

# Test binary, build with `go test -c`
*.test
Expand Down
10 changes: 6 additions & 4 deletions cmd/diagnosis/main.go
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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))
}
16 changes: 8 additions & 8 deletions diagnosis/fetching.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package diagnosis

import (
"log"
"net"
"os"

"github.com/golang/glog"
"github.com/matishsiao/goInfo"
)

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions diagnosis/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package diagnosis

import (
"encoding/json"
"log"

"github.com/golang/glog"
)

// Info includes information for diagnosis.
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 9871515

Please sign in to comment.