diff --git a/core/discov/facade.go b/core/discov/facade.go index 2b7497a6..2a7838f2 100644 --- a/core/discov/facade.go +++ b/core/discov/facade.go @@ -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 ( @@ -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 } diff --git a/core/lang/lang.go b/core/lang/lang.go index cf02ad6e..901c5604 100644 --- a/core/lang/lang.go +++ b/core/lang/lang.go @@ -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) - } -} diff --git a/core/lang/lang_test.go b/core/lang/lang_test.go deleted file mode 100644 index 6b3c6417..00000000 --- a/core/lang/lang_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package lang - -import "testing" - -func TestMust(t *testing.T) { - Must(nil) -} diff --git a/core/logx/logs.go b/core/logx/logs.go index edb71040..b3ac081c 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -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" ) @@ -46,6 +45,7 @@ const ( levelInfo = "info" levelError = "error" levelSevere = "severe" + levelFatal = "fatal" levelSlow = "slow" levelStat = "stat" @@ -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. @@ -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) } diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index d8cb7dbc..ede9d505 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -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 diff --git a/core/stat/internal/cpu_linux.go b/core/stat/internal/cpu_linux.go index fea314b7..460d3b99 100644 --- a/core/stat/internal/cpu_linux.go +++ b/core/stat/internal/cpu_linux.go @@ -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 ( @@ -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 { @@ -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 { diff --git a/core/stores/internal/cleaner.go b/core/stores/internal/cleaner.go index 3838207a..5d4b8656 100644 --- a/core/stores/internal/cleaner.go +++ b/core/stores/internal/cleaner.go @@ -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" @@ -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) diff --git a/core/sysx/host.go b/core/sysx/host.go index dccddf9f..8cfb54a1 100644 --- a/core/sysx/host.go +++ b/core/sysx/host.go @@ -3,7 +3,7 @@ package sysx import ( "os" - "github.com/tal-tech/go-zero/core/lang" + "github.com/tal-tech/go-zero/core/stringx" ) var hostname string @@ -11,7 +11,9 @@ var hostname string func init() { var err error hostname, err = os.Hostname() - lang.Must(err) + if err != nil { + hostname = stringx.RandId() + } } func Hostname() string { diff --git a/example/breaker/main.go b/example/breaker/main.go index 8bfa30f5..23cae92e 100644 --- a/example/breaker/main.go +++ b/example/breaker/main.go @@ -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" ) @@ -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") diff --git a/example/etcd/sub/sub.go b/example/etcd/sub/sub.go index 559737ca..284f7f3a 100644 --- a/example/etcd/sub/sub.go +++ b/example/etcd/sub/sub.go @@ -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() diff --git a/example/http/breaker/client/client.go b/example/http/breaker/client/client.go index a756c3e1..131f14f9 100644 --- a/example/http/breaker/client/client.go +++ b/example/http/breaker/client/client.go @@ -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" ) @@ -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() { diff --git a/example/load/main.go b/example/load/main.go index efefbfae..d4d6f8a0 100644 --- a/example/load/main.go +++ b/example/load/main.go @@ -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" ) @@ -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") diff --git a/example/load/simulate/client/main.go b/example/load/simulate/client/main.go index da98c97a..529d2f87 100644 --- a/example/load/simulate/client/main.go +++ b/example/load/simulate/client/main.go @@ -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 ( @@ -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") diff --git a/tools/goctl/api/dartgen/gen.go b/tools/goctl/api/dartgen/gen.go index 68a48124..6ee7623a 100644 --- a/tools/goctl/api/dartgen/gen.go +++ b/tools/goctl/api/dartgen/gen.go @@ -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" ) @@ -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 } diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 0b9c553a..80209f28 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -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" @@ -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) diff --git a/tools/goctl/api/javagen/gen.go b/tools/goctl/api/javagen/gen.go index 086e7dce..420265ef 100644 --- a/tools/goctl/api/javagen/gen.go +++ b/tools/goctl/api/javagen/gen.go @@ -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" @@ -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 diff --git a/tools/goctl/api/main.go b/tools/goctl/api/main.go index 758d23ca..53cf4d0f 100644 --- a/tools/goctl/api/main.go +++ b/tools/goctl/api/main.go @@ -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" ) @@ -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) } diff --git a/tools/goctl/api/tsgen/gen.go b/tools/goctl/api/tsgen/gen.go index e21b085b..579378d9 100644 --- a/tools/goctl/api/tsgen/gen.go +++ b/tools/goctl/api/tsgen/gen.go @@ -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" @@ -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 diff --git a/tools/goctl/api/util/util.go b/tools/goctl/api/util/util.go index c4ec3256..afc77a55 100644 --- a/tools/goctl/api/util/util.go +++ b/tools/goctl/api/util/util.go @@ -8,13 +8,13 @@ import ( "path" "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/util" ) 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) if util.FileExists(fpath) { fmt.Printf("%s exists, ignored generation\n", fpath) diff --git a/tools/goctl/update/update.go b/tools/goctl/update/update.go index 480f8ab4..5b1e42b3 100644 --- a/tools/goctl/update/update.go +++ b/tools/goctl/update/update.go @@ -8,7 +8,6 @@ import ( "github.com/tal-tech/go-zero/core/conf" "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/tools/goctl/update/config" "github.com/tal-tech/go-zero/tools/goctl/util" @@ -56,5 +55,5 @@ func main() { fs := http.FileServer(http.Dir(c.FileDir)) 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)) } diff --git a/tools/modelctl/model/modelgen/gen.go b/tools/modelctl/model/modelgen/gen.go index 8601bc8d..c30f7cea 100644 --- a/tools/modelctl/model/modelgen/gen.go +++ b/tools/modelctl/model/modelgen/gen.go @@ -5,7 +5,7 @@ import ( "fmt" "strings" - "github.com/tal-tech/go-zero/core/lang" + "github.com/tal-tech/go-zero/core/logx" "github.com/urfave/cli" ) @@ -14,7 +14,7 @@ func FileModelCommand(c *cli.Context) error { if len(configFile) == 0 { return errors.New("missing config value") } - lang.Must(genModelWithConfigFile(configFile)) + logx.Must(genModelWithConfigFile(configFile)) return nil } @@ -36,6 +36,6 @@ func CmdModelCommand(c *cli.Context) error { user := addressArr[0] host := addressArr[1] 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 }