Skip to content

Commit

Permalink
move lang.Must into logx.Must to make sure output fatal message as json
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Aug 14, 2020
1 parent 9d9399a commit 8745039
Show file tree
Hide file tree
Showing 21 changed files with 69 additions and 70 deletions.
4 changes: 2 additions & 2 deletions core/discov/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package discov

import (
"github.com/tal-tech/go-zero/core/discov/internal"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
)

type (
Expand All @@ -26,7 +26,7 @@ func NewFacade(endpoints []string) Facade {

func (f Facade) Client() internal.EtcdClient {
conn, err := f.registry.GetConn(f.endpoints)
lang.Must(err)
logx.Must(err)
return conn
}

Expand Down
8 changes: 0 additions & 8 deletions core/lang/lang.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package lang

import "log"

var Placeholder PlaceholderType

type (
GenericType = interface{}
PlaceholderType = struct{}
)

func Must(err error) {
if err != nil {
log.Fatal(err)
}
}
7 changes: 0 additions & 7 deletions core/lang/lang_test.go

This file was deleted.

12 changes: 10 additions & 2 deletions core/logx/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"sync/atomic"

"github.com/tal-tech/go-zero/core/iox"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/sysx"
"github.com/tal-tech/go-zero/core/timex"
)
Expand Down Expand Up @@ -46,6 +45,7 @@ const (
levelInfo = "info"
levelError = "error"
levelSevere = "severe"
levelFatal = "fatal"
levelSlow = "slow"
levelStat = "stat"

Expand Down Expand Up @@ -100,7 +100,7 @@ type (
)

func MustSetup(c LogConf) {
lang.Must(SetUp(c))
Must(SetUp(c))
}

// SetUp sets up the logx. If already set up, just return nil.
Expand Down Expand Up @@ -210,6 +210,14 @@ func Infof(format string, v ...interface{}) {
infoSync(fmt.Sprintf(format, v...))
}

func Must(err error) {
if err != nil {
msg := formatWithCaller(err.Error(), 3)
output(severeLog, levelFatal, msg)
os.Exit(1)
}
}

func SetLevel(level uint32) {
atomic.StoreUint32(&logLevel, level)
}
Expand Down
4 changes: 4 additions & 0 deletions core/logx/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func TestSetLevelWithDuration(t *testing.T) {
assert.Equal(t, 0, writer.builder.Len())
}

func TestMustNil(t *testing.T) {
Must(nil)
}

func BenchmarkCopyByteSliceAppend(b *testing.B) {
for i := 0; i < b.N; i++ {
var buf []byte
Expand Down
12 changes: 6 additions & 6 deletions core/stat/internal/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/tal-tech/go-zero/core/iox"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
)

const (
Expand All @@ -24,17 +24,17 @@ var (

func init() {
cpus, err := perCpuUsage()
lang.Must(err)
logx.Must(err)
cores = uint64(len(cpus))

sets, err := cpuSets()
lang.Must(err)
logx.Must(err)
quota = float64(len(sets))
cq, err := cpuQuota()
if err == nil {
if cq != -1 {
period, err := cpuPeriod()
lang.Must(err)
logx.Must(err)

limit := float64(cq) / float64(period)
if limit < quota {
Expand All @@ -44,10 +44,10 @@ func init() {
}

preSystem, err = systemCpuUsage()
lang.Must(err)
logx.Must(err)

preTotal, err = totalCpuUsage()
lang.Must(err)
logx.Must(err)
}

func RefreshCpu() uint64 {
Expand Down
3 changes: 1 addition & 2 deletions core/stores/internal/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/proc"
"github.com/tal-tech/go-zero/core/stat"
Expand Down Expand Up @@ -33,7 +32,7 @@ type delayTask struct {
func init() {
var err error
timingWheel, err = collection.NewTimingWheel(time.Second, timingWheelSlots, clean)
lang.Must(err)
logx.Must(err)

proc.AddShutdownListener(func() {
timingWheel.Drain(clean)
Expand Down
6 changes: 4 additions & 2 deletions core/sysx/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package sysx
import (
"os"

"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/stringx"
)

var hostname string

func init() {
var err error
hostname, err = os.Hostname()
lang.Must(err)
if err != nil {
hostname = stringx.RandId()
}
}

func Hostname() string {
Expand Down
3 changes: 2 additions & 1 deletion example/breaker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/tal-tech/go-zero/core/breaker"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"gopkg.in/cheggaaa/pb.v1"
)

Expand Down Expand Up @@ -99,7 +100,7 @@ func main() {

gb := breaker.NewBreaker()
fp, err := os.Create("result.csv")
lang.Must(err)
logx.Must(err)
defer fp.Close()
fmt.Fprintln(fp, "seconds,state,googleCalls,netflixCalls")

Expand Down
4 changes: 2 additions & 2 deletions example/etcd/sub/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"time"

"github.com/tal-tech/go-zero/core/discov"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
)

func main() {
sub, err := discov.NewSubscriber([]string{"etcd.discovery:2379"}, "028F2C35852D", discov.Exclusive())
lang.Must(err)
logx.Must(err)

ticker := time.NewTicker(time.Second * 3)
defer ticker.Stop()
Expand Down
5 changes: 3 additions & 2 deletions example/http/breaker/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/threading"
"gopkg.in/cheggaaa/pb.v1"
)
Expand Down Expand Up @@ -119,14 +120,14 @@ func main() {
flag.Parse()

fp, err := os.Create("result.csv")
lang.Must(err)
logx.Must(err)
defer fp.Close()
fmt.Fprintln(fp, "seconds,goodOk,goodFail,goodReject,goodErrs,goodUnknowns,goodDropRatio,"+
"heavyOk,heavyFail,heavyReject,heavyErrs,heavyUnknowns,heavyDropRatio")

var gm, hm metric
dur, err := time.ParseDuration(*duration)
lang.Must(err)
logx.Must(err)
done := make(chan lang.PlaceholderType)
group := threading.NewRoutineGroup()
group.RunSafe(func() {
Expand Down
4 changes: 2 additions & 2 deletions example/load/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/core/executors"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/syncx"
"gopkg.in/cheggaaa/pb.v1"
)
Expand Down Expand Up @@ -47,7 +47,7 @@ func main() {
lessWriter = executors.NewLessExecutor(interval * total / 100)

fp, err := os.Create("result.csv")
lang.Must(err)
logx.Must(err)
defer fp.Close()
fmt.Fprintln(fp, "second,maxFlight,flying,agressiveAvgFlying,lazyAvgFlying,bothAvgFlying")

Expand Down
4 changes: 2 additions & 2 deletions example/load/simulate/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

"github.com/tal-tech/go-zero/core/fx"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
)

var (
Expand All @@ -27,7 +27,7 @@ func main() {
flag.Parse()

fp, err := os.Create("result.csv")
lang.Must(err)
logx.Must(err)
defer fp.Close()
fmt.Fprintln(fp, "seconds,total,pass,fail,drop")

Expand Down
8 changes: 4 additions & 4 deletions tools/goctl/api/dartgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"strings"

"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -32,8 +32,8 @@ func DartCommand(c *cli.Context) error {
dir = dir + "/"
}
api.Info.Title = strings.Replace(apiFile, ".api", "", -1)
lang.Must(genData(dir+"data/", api))
lang.Must(genApi(dir+"api/", api))
lang.Must(genVars(dir + "vars/"))
logx.Must(genData(dir+"data/", api))
logx.Must(genApi(dir+"api/", api))
logx.Must(genVars(dir + "vars/"))
return nil
}
20 changes: 10 additions & 10 deletions tools/goctl/api/gogen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"

"github.com/logrusorgru/aurora"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
apiformat "github.com/tal-tech/go-zero/tools/goctl/api/format"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
Expand Down Expand Up @@ -45,15 +45,15 @@ func GoCommand(c *cli.Context) error {
return err
}

lang.Must(util.MkdirIfNotExist(dir))
lang.Must(genEtc(dir, api))
lang.Must(genConfig(dir))
lang.Must(genMain(dir, api))
lang.Must(genServiceContext(dir, api))
lang.Must(genTypes(dir, api))
lang.Must(genHandlers(dir, api))
lang.Must(genRoutes(dir, api))
lang.Must(genLogic(dir, api))
logx.Must(util.MkdirIfNotExist(dir))
logx.Must(genEtc(dir, api))
logx.Must(genConfig(dir))
logx.Must(genMain(dir, api))
logx.Must(genServiceContext(dir, api))
logx.Must(genTypes(dir, api))
logx.Must(genHandlers(dir, api))
logx.Must(genRoutes(dir, api))
logx.Must(genLogic(dir, api))
// it does not work
format(dir)
createGoModFileIfNeed(dir)
Expand Down
8 changes: 4 additions & 4 deletions tools/goctl/api/javagen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/logrusorgru/aurora"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
Expand Down Expand Up @@ -36,9 +36,9 @@ func JavaCommand(c *cli.Context) error {
packetName = packetName[:len(packetName)-4]
}

lang.Must(util.MkdirIfNotExist(dir))
lang.Must(genPacket(dir, packetName, api))
lang.Must(genComponents(dir, packetName, api))
logx.Must(util.MkdirIfNotExist(dir))
logx.Must(genPacket(dir, packetName, api))
logx.Must(genComponents(dir, packetName, api))

fmt.Println(aurora.Green("Done."))
return nil
Expand Down
6 changes: 3 additions & 3 deletions tools/goctl/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
)

Expand All @@ -14,8 +14,8 @@ func main() {
}

p, err := parser.NewParser(os.Args[1])
lang.Must(err)
logx.Must(err)
api, err := p.Parse()
lang.Must(err)
logx.Must(err)
fmt.Println(api)
}
8 changes: 4 additions & 4 deletions tools/goctl/api/tsgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/logrusorgru/aurora"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
Expand Down Expand Up @@ -34,9 +34,9 @@ func TsCommand(c *cli.Context) error {
return err
}

lang.Must(util.MkdirIfNotExist(dir))
lang.Must(genHandler(dir, webApi, caller, api, unwrapApi))
lang.Must(genComponents(dir, api))
logx.Must(util.MkdirIfNotExist(dir))
logx.Must(genHandler(dir, webApi, caller, api, unwrapApi))
logx.Must(genComponents(dir, api))

fmt.Println(aurora.Green("Done."))
return nil
Expand Down
Loading

0 comments on commit 8745039

Please sign in to comment.