move lang.Must into logx.Must to make sure output fatal message as json

master v1.0.3
kevin 4 years ago
parent 9d9399ad10
commit 8745039877

@ -2,7 +2,7 @@ package discov
import ( import (
"github.com/tal-tech/go-zero/core/discov/internal" "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 ( type (
@ -26,7 +26,7 @@ func NewFacade(endpoints []string) Facade {
func (f Facade) Client() internal.EtcdClient { func (f Facade) Client() internal.EtcdClient {
conn, err := f.registry.GetConn(f.endpoints) conn, err := f.registry.GetConn(f.endpoints)
lang.Must(err) logx.Must(err)
return conn return conn
} }

@ -1,16 +1,8 @@
package lang package lang
import "log"
var Placeholder PlaceholderType var Placeholder PlaceholderType
type ( type (
GenericType = interface{} GenericType = interface{}
PlaceholderType = struct{} PlaceholderType = struct{}
) )
func Must(err error) {
if err != nil {
log.Fatal(err)
}
}

@ -1,7 +0,0 @@
package lang
import "testing"
func TestMust(t *testing.T) {
Must(nil)
}

@ -17,7 +17,6 @@ import (
"sync/atomic" "sync/atomic"
"github.com/tal-tech/go-zero/core/iox" "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/sysx"
"github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/timex"
) )
@ -46,6 +45,7 @@ const (
levelInfo = "info" levelInfo = "info"
levelError = "error" levelError = "error"
levelSevere = "severe" levelSevere = "severe"
levelFatal = "fatal"
levelSlow = "slow" levelSlow = "slow"
levelStat = "stat" levelStat = "stat"
@ -100,7 +100,7 @@ type (
) )
func MustSetup(c LogConf) { func MustSetup(c LogConf) {
lang.Must(SetUp(c)) Must(SetUp(c))
} }
// SetUp sets up the logx. If already set up, just return nil. // SetUp sets up the logx. If already set up, just return nil.
@ -210,6 +210,14 @@ func Infof(format string, v ...interface{}) {
infoSync(fmt.Sprintf(format, v...)) 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) { func SetLevel(level uint32) {
atomic.StoreUint32(&logLevel, level) atomic.StoreUint32(&logLevel, level)
} }

@ -131,6 +131,10 @@ func TestSetLevelWithDuration(t *testing.T) {
assert.Equal(t, 0, writer.builder.Len()) assert.Equal(t, 0, writer.builder.Len())
} }
func TestMustNil(t *testing.T) {
Must(nil)
}
func BenchmarkCopyByteSliceAppend(b *testing.B) { func BenchmarkCopyByteSliceAppend(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
var buf []byte var buf []byte

@ -7,7 +7,7 @@ import (
"time" "time"
"github.com/tal-tech/go-zero/core/iox" "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 ( const (
@ -24,17 +24,17 @@ var (
func init() { func init() {
cpus, err := perCpuUsage() cpus, err := perCpuUsage()
lang.Must(err) logx.Must(err)
cores = uint64(len(cpus)) cores = uint64(len(cpus))
sets, err := cpuSets() sets, err := cpuSets()
lang.Must(err) logx.Must(err)
quota = float64(len(sets)) quota = float64(len(sets))
cq, err := cpuQuota() cq, err := cpuQuota()
if err == nil { if err == nil {
if cq != -1 { if cq != -1 {
period, err := cpuPeriod() period, err := cpuPeriod()
lang.Must(err) logx.Must(err)
limit := float64(cq) / float64(period) limit := float64(cq) / float64(period)
if limit < quota { if limit < quota {
@ -44,10 +44,10 @@ func init() {
} }
preSystem, err = systemCpuUsage() preSystem, err = systemCpuUsage()
lang.Must(err) logx.Must(err)
preTotal, err = totalCpuUsage() preTotal, err = totalCpuUsage()
lang.Must(err) logx.Must(err)
} }
func RefreshCpu() uint64 { func RefreshCpu() uint64 {

@ -5,7 +5,6 @@ import (
"time" "time"
"github.com/tal-tech/go-zero/core/collection" "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/logx"
"github.com/tal-tech/go-zero/core/proc" "github.com/tal-tech/go-zero/core/proc"
"github.com/tal-tech/go-zero/core/stat" "github.com/tal-tech/go-zero/core/stat"
@ -33,7 +32,7 @@ type delayTask struct {
func init() { func init() {
var err error var err error
timingWheel, err = collection.NewTimingWheel(time.Second, timingWheelSlots, clean) timingWheel, err = collection.NewTimingWheel(time.Second, timingWheelSlots, clean)
lang.Must(err) logx.Must(err)
proc.AddShutdownListener(func() { proc.AddShutdownListener(func() {
timingWheel.Drain(clean) timingWheel.Drain(clean)

@ -3,7 +3,7 @@ package sysx
import ( import (
"os" "os"
"github.com/tal-tech/go-zero/core/lang" "github.com/tal-tech/go-zero/core/stringx"
) )
var hostname string var hostname string
@ -11,7 +11,9 @@ var hostname string
func init() { func init() {
var err error var err error
hostname, err = os.Hostname() hostname, err = os.Hostname()
lang.Must(err) if err != nil {
hostname = stringx.RandId()
}
} }
func Hostname() string { func Hostname() string {

@ -10,6 +10,7 @@ import (
"github.com/tal-tech/go-zero/core/breaker" "github.com/tal-tech/go-zero/core/breaker"
"github.com/tal-tech/go-zero/core/lang" "github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"gopkg.in/cheggaaa/pb.v1" "gopkg.in/cheggaaa/pb.v1"
) )
@ -99,7 +100,7 @@ func main() {
gb := breaker.NewBreaker() gb := breaker.NewBreaker()
fp, err := os.Create("result.csv") fp, err := os.Create("result.csv")
lang.Must(err) logx.Must(err)
defer fp.Close() defer fp.Close()
fmt.Fprintln(fp, "seconds,state,googleCalls,netflixCalls") fmt.Fprintln(fp, "seconds,state,googleCalls,netflixCalls")

@ -5,12 +5,12 @@ import (
"time" "time"
"github.com/tal-tech/go-zero/core/discov" "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() { func main() {
sub, err := discov.NewSubscriber([]string{"etcd.discovery:2379"}, "028F2C35852D", discov.Exclusive()) sub, err := discov.NewSubscriber([]string{"etcd.discovery:2379"}, "028F2C35852D", discov.Exclusive())
lang.Must(err) logx.Must(err)
ticker := time.NewTicker(time.Second * 3) ticker := time.NewTicker(time.Second * 3)
defer ticker.Stop() defer ticker.Stop()

@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/tal-tech/go-zero/core/lang" "github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/threading" "github.com/tal-tech/go-zero/core/threading"
"gopkg.in/cheggaaa/pb.v1" "gopkg.in/cheggaaa/pb.v1"
) )
@ -119,14 +120,14 @@ func main() {
flag.Parse() flag.Parse()
fp, err := os.Create("result.csv") fp, err := os.Create("result.csv")
lang.Must(err) logx.Must(err)
defer fp.Close() defer fp.Close()
fmt.Fprintln(fp, "seconds,goodOk,goodFail,goodReject,goodErrs,goodUnknowns,goodDropRatio,"+ fmt.Fprintln(fp, "seconds,goodOk,goodFail,goodReject,goodErrs,goodUnknowns,goodDropRatio,"+
"heavyOk,heavyFail,heavyReject,heavyErrs,heavyUnknowns,heavyDropRatio") "heavyOk,heavyFail,heavyReject,heavyErrs,heavyUnknowns,heavyDropRatio")
var gm, hm metric var gm, hm metric
dur, err := time.ParseDuration(*duration) dur, err := time.ParseDuration(*duration)
lang.Must(err) logx.Must(err)
done := make(chan lang.PlaceholderType) done := make(chan lang.PlaceholderType)
group := threading.NewRoutineGroup() group := threading.NewRoutineGroup()
group.RunSafe(func() { group.RunSafe(func() {

@ -13,7 +13,7 @@ import (
"github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/core/executors" "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" "github.com/tal-tech/go-zero/core/syncx"
"gopkg.in/cheggaaa/pb.v1" "gopkg.in/cheggaaa/pb.v1"
) )
@ -47,7 +47,7 @@ func main() {
lessWriter = executors.NewLessExecutor(interval * total / 100) lessWriter = executors.NewLessExecutor(interval * total / 100)
fp, err := os.Create("result.csv") fp, err := os.Create("result.csv")
lang.Must(err) logx.Must(err)
defer fp.Close() defer fp.Close()
fmt.Fprintln(fp, "second,maxFlight,flying,agressiveAvgFlying,lazyAvgFlying,bothAvgFlying") fmt.Fprintln(fp, "second,maxFlight,flying,agressiveAvgFlying,lazyAvgFlying,bothAvgFlying")

@ -11,7 +11,7 @@ import (
"time" "time"
"github.com/tal-tech/go-zero/core/fx" "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 ( var (
@ -27,7 +27,7 @@ func main() {
flag.Parse() flag.Parse()
fp, err := os.Create("result.csv") fp, err := os.Create("result.csv")
lang.Must(err) logx.Must(err)
defer fp.Close() defer fp.Close()
fmt.Fprintln(fp, "seconds,total,pass,fail,drop") fmt.Fprintln(fp, "seconds,total,pass,fail,drop")

@ -4,7 +4,7 @@ import (
"errors" "errors"
"strings" "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/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -32,8 +32,8 @@ func DartCommand(c *cli.Context) error {
dir = dir + "/" dir = dir + "/"
} }
api.Info.Title = strings.Replace(apiFile, ".api", "", -1) api.Info.Title = strings.Replace(apiFile, ".api", "", -1)
lang.Must(genData(dir+"data/", api)) logx.Must(genData(dir+"data/", api))
lang.Must(genApi(dir+"api/", api)) logx.Must(genApi(dir+"api/", api))
lang.Must(genVars(dir + "vars/")) logx.Must(genVars(dir + "vars/"))
return nil return nil
} }

@ -14,7 +14,7 @@ import (
"time" "time"
"github.com/logrusorgru/aurora" "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" apiformat "github.com/tal-tech/go-zero/tools/goctl/api/format"
"github.com/tal-tech/go-zero/tools/goctl/api/parser" "github.com/tal-tech/go-zero/tools/goctl/api/parser"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util" apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
@ -45,15 +45,15 @@ func GoCommand(c *cli.Context) error {
return err return err
} }
lang.Must(util.MkdirIfNotExist(dir)) logx.Must(util.MkdirIfNotExist(dir))
lang.Must(genEtc(dir, api)) logx.Must(genEtc(dir, api))
lang.Must(genConfig(dir)) logx.Must(genConfig(dir))
lang.Must(genMain(dir, api)) logx.Must(genMain(dir, api))
lang.Must(genServiceContext(dir, api)) logx.Must(genServiceContext(dir, api))
lang.Must(genTypes(dir, api)) logx.Must(genTypes(dir, api))
lang.Must(genHandlers(dir, api)) logx.Must(genHandlers(dir, api))
lang.Must(genRoutes(dir, api)) logx.Must(genRoutes(dir, api))
lang.Must(genLogic(dir, api)) logx.Must(genLogic(dir, api))
// it does not work // it does not work
format(dir) format(dir)
createGoModFileIfNeed(dir) createGoModFileIfNeed(dir)

@ -6,7 +6,7 @@ import (
"strings" "strings"
"github.com/logrusorgru/aurora" "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/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -36,9 +36,9 @@ func JavaCommand(c *cli.Context) error {
packetName = packetName[:len(packetName)-4] packetName = packetName[:len(packetName)-4]
} }
lang.Must(util.MkdirIfNotExist(dir)) logx.Must(util.MkdirIfNotExist(dir))
lang.Must(genPacket(dir, packetName, api)) logx.Must(genPacket(dir, packetName, api))
lang.Must(genComponents(dir, packetName, api)) logx.Must(genComponents(dir, packetName, api))
fmt.Println(aurora.Green("Done.")) fmt.Println(aurora.Green("Done."))
return nil return nil

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"os" "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" "github.com/tal-tech/go-zero/tools/goctl/api/parser"
) )
@ -14,8 +14,8 @@ func main() {
} }
p, err := parser.NewParser(os.Args[1]) p, err := parser.NewParser(os.Args[1])
lang.Must(err) logx.Must(err)
api, err := p.Parse() api, err := p.Parse()
lang.Must(err) logx.Must(err)
fmt.Println(api) fmt.Println(api)
} }

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"github.com/logrusorgru/aurora" "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/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -34,9 +34,9 @@ func TsCommand(c *cli.Context) error {
return err return err
} }
lang.Must(util.MkdirIfNotExist(dir)) logx.Must(util.MkdirIfNotExist(dir))
lang.Must(genHandler(dir, webApi, caller, api, unwrapApi)) logx.Must(genHandler(dir, webApi, caller, api, unwrapApi))
lang.Must(genComponents(dir, api)) logx.Must(genComponents(dir, api))
fmt.Println(aurora.Green("Done.")) fmt.Println(aurora.Green("Done."))
return nil return nil

@ -8,13 +8,13 @@ import (
"path" "path"
"strings" "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/spec" "github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util"
) )
func MaybeCreateFile(dir, subdir, file string) (fp *os.File, created bool, err error) { func MaybeCreateFile(dir, subdir, file string) (fp *os.File, created bool, err error) {
lang.Must(util.MkdirIfNotExist(path.Join(dir, subdir))) logx.Must(util.MkdirIfNotExist(path.Join(dir, subdir)))
fpath := path.Join(dir, subdir, file) fpath := path.Join(dir, subdir, file)
if util.FileExists(fpath) { if util.FileExists(fpath) {
fmt.Printf("%s exists, ignored generation\n", fpath) fmt.Printf("%s exists, ignored generation\n", fpath)

@ -8,7 +8,6 @@ import (
"github.com/tal-tech/go-zero/core/conf" "github.com/tal-tech/go-zero/core/conf"
"github.com/tal-tech/go-zero/core/hash" "github.com/tal-tech/go-zero/core/hash"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/tools/goctl/update/config" "github.com/tal-tech/go-zero/tools/goctl/update/config"
"github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util"
@ -56,5 +55,5 @@ func main() {
fs := http.FileServer(http.Dir(c.FileDir)) fs := http.FileServer(http.Dir(c.FileDir))
http.Handle(c.FilePath, http.StripPrefix(c.FilePath, forChksumHandler(path.Join(c.FileDir, filename), fs))) http.Handle(c.FilePath, http.StripPrefix(c.FilePath, forChksumHandler(path.Join(c.FileDir, filename), fs)))
lang.Must(http.ListenAndServe(c.ListenOn, nil)) logx.Must(http.ListenAndServe(c.ListenOn, nil))
} }

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/tal-tech/go-zero/core/lang" "github.com/tal-tech/go-zero/core/logx"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -14,7 +14,7 @@ func FileModelCommand(c *cli.Context) error {
if len(configFile) == 0 { if len(configFile) == 0 {
return errors.New("missing config value") return errors.New("missing config value")
} }
lang.Must(genModelWithConfigFile(configFile)) logx.Must(genModelWithConfigFile(configFile))
return nil return nil
} }
@ -36,6 +36,6 @@ func CmdModelCommand(c *cli.Context) error {
user := addressArr[0] user := addressArr[0]
host := addressArr[1] host := addressArr[1]
address = fmt.Sprintf("%v@tcp(%v)/information_schema", user, host) address = fmt.Sprintf("%v@tcp(%v)/information_schema", user, host)
lang.Must(genModelWithDataSource(address, schema, force, redis, nil)) logx.Must(genModelWithDataSource(address, schema, force, redis, nil))
return nil return nil
} }

Loading…
Cancel
Save