diff --git a/common/types.go b/common/types.go index a2eaec2..1918975 100644 --- a/common/types.go +++ b/common/types.go @@ -8,7 +8,7 @@ import ( _ "github.com/go-sql-driver/mysql" // _ "github.com/lib/pq" - "github.com/sirupsen/logrus" + "gorm.io/driver/mysql" // "gorm.io/driver/postgres" @@ -78,7 +78,7 @@ func (op *tracePlugin) Initialize(db *gorm.DB) (err error) { after := func(db *gorm.DB) { _ts, _ := db.InstanceGet("ivy.startTime") sql := db.Dialector.Explain(db.Statement.SQL.String(), db.Statement.Vars...) - logrus.Printf("used: %d ms affected: %d sql is: %s", time.Since(_ts.(time.Time)).Milliseconds(), db.RowsAffected, sql) + Logf("used: %d ms affected: %d sql is: %s", time.Since(_ts.(time.Time)).Milliseconds(), db.RowsAffected, sql) if v, ok := db.InstanceGet("ivy.must"); ok && v.(bool) { if db.Error != nil && db.Error != gorm.ErrRecordNotFound { panic(db.Error) @@ -89,7 +89,7 @@ func (op *tracePlugin) Initialize(db *gorm.DB) (err error) { beforeName := "cb_before" afterName := "cb_after" - logrus.Printf("installing db plugin: %s", op.Name()) + Logf("installing db plugin: %s", op.Name()) // 开始前 _ = db.Callback().Create().Before("gorm:before_create").Register(beforeName, before) _ = db.Callback().Query().Before("gorm:query").Register(beforeName, before) @@ -126,7 +126,7 @@ func GetDsn(conf map[string]string) string { func DbGet(conf map[string]string) *DB { dsn := GetDsn(conf) if dbs[dsn] == nil { - logrus.Printf("connecting %s", strings.Replace(dsn, conf["password"], "****", 1)) + Logf("connecting %s", strings.Replace(dsn, conf["password"], "****", 1)) db1, err := gorm.Open(getGormDialator(conf["driver"], dsn), &gorm.Config{ SkipDefaultTransaction: true, }) @@ -149,7 +149,7 @@ func SdbGet(conf map[string]string) *sql.DB { // SdbAlone get a standalone db connection func SdbAlone(conf map[string]string) *sql.DB { dsn := GetDsn(conf) - logrus.Printf("opening alone %s: %s", conf["driver"], strings.Replace(dsn, conf["password"], "****", 1)) + Logf("opening alone %s: %s", conf["driver"], strings.Replace(dsn, conf["password"], "****", 1)) mdb, err := sql.Open(conf["driver"], dsn) E2P(err) return mdb @@ -160,9 +160,9 @@ func SdbExec(db *sql.DB, sql string, values ...interface{}) (affected int64, rer r, rerr := db.Exec(sql, values...) if rerr == nil { affected, rerr = r.RowsAffected() - logrus.Printf("affected: %d for %s %v", affected, sql, values) + Logf("affected: %d for %s %v", affected, sql, values) } else { - RedLogf("exec error: %v for %s %v", rerr, sql, values) + LogRedf("exec error: %v for %s %v", rerr, sql, values) } return } @@ -172,15 +172,15 @@ func StxExec(tx *sql.Tx, sql string, values ...interface{}) (affected int64, rer r, rerr := tx.Exec(sql, values...) if rerr == nil { affected, rerr = r.RowsAffected() - logrus.Printf("affected: %d for %s %v", affected, sql, values) + Logf("affected: %d for %s %v", affected, sql, values) } else { - RedLogf("exec error: %v for %s %v", rerr, sql, values) + LogRedf("exec error: %v for %s %v", rerr, sql, values) } return } // StxQueryRow use raw tx to query row func StxQueryRow(tx *sql.Tx, query string, args ...interface{}) *sql.Row { - logrus.Printf("querying: "+query, args...) + Logf("querying: "+query, args...) return tx.QueryRow(query, args...) } diff --git a/common/utils.go b/common/utils.go index 65377db..9e6dc8d 100644 --- a/common/utils.go +++ b/common/utils.go @@ -125,9 +125,9 @@ func GetGinApp() *gin.Engine { } } began := time.Now() - logrus.Printf("begin %s %s query: %s body: %s", c.Request.Method, c.FullPath(), c.Request.URL.RawQuery, body) + Logf("begin %s %s query: %s body: %s", c.Request.Method, c.FullPath(), c.Request.URL.RawQuery, body) c.Next() - logrus.Printf("used %d ms %s %s query: %s body: %s", time.Since(began).Milliseconds(), c.Request.Method, c.FullPath(), c.Request.URL.RawQuery, body) + Logf("used %d ms %s %s query: %s body: %s", time.Since(began).Milliseconds(), c.Request.Method, c.FullPath(), c.Request.URL.RawQuery, body) }) app.Any("/api/ping", func(c *gin.Context) { c.JSON(200, M{"msg": "pong"}) }) @@ -145,10 +145,10 @@ func WrapHandler(fn func(*gin.Context) (interface{}, error)) gin.HandlerFunc { b, err = json.Marshal(r) } if err != nil { - logrus.Printf("status: 500, code: 500 message: %s", err.Error()) + Logf("status: 500, code: 500 message: %s", err.Error()) c.JSON(500, M{"code": 500, "message": err.Error()}) } else { - logrus.Printf("status: 200, content: %s", string(b)) + Logf("status: 200, content: %s", string(b)) c.Status(200) c.Writer.Header().Add("Content-Type", "application/json") _, err = c.Writer.Write(b) @@ -166,12 +166,12 @@ func init() { // RestyClient.SetRetryWaitTime(1 * time.Second) RestyClient.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error { r.URL = MayReplaceLocalhost(r.URL) - logrus.Printf("requesting: %s %s %v %v", r.Method, r.URL, r.Body, r.QueryParam) + Logf("requesting: %s %s %v %v", r.Method, r.URL, r.Body, r.QueryParam) return nil }) RestyClient.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error { r := resp.Request - logrus.Printf("requested: %s %s %s", r.Method, r.URL, resp.String()) + Logf("requested: %s %s %s", r.Method, r.URL, resp.String()) return nil }) } @@ -184,15 +184,9 @@ func CheckRestySuccess(resp *resty.Response, err error) { } } -// formatter 自定义formatter -type formatter struct{} - -// Format 进行格式化 -func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) { - var b *bytes.Buffer = &bytes.Buffer{} - if entry.Buffer != nil { - b = entry.Buffer - } +// Logf 输出日志 +func Logf(format string, args ...interface{}) { + msg := fmt.Sprintf(format, args...) n := time.Now() ts := fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d.%03d", n.Year(), n.Month(), n.Day(), n.Hour(), n.Minute(), n.Second(), n.Nanosecond()/1000000) var file string @@ -203,23 +197,21 @@ func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) { break } } - b.WriteString(fmt.Sprintf("%s %s:%d %s\n", ts, path.Base(file), line, entry.Message)) - return b.Bytes(), nil + fmt.Printf("%s %s:%d %s\n", ts, path.Base(file), line, msg) } -// RedLogf 采用红色打印错误类信息 -func RedLogf(fmt string, args ...interface{}) { +// LogRedf 采用红色打印错误类信息 +func LogRedf(fmt string, args ...interface{}) { logrus.Errorf("\x1b[31m\n"+fmt+"\x1b[0m\n", args...) } // InitConfig init config func InitConfig(dir string, config interface{}) { - logrus.SetFormatter(&formatter{}) cont, err := ioutil.ReadFile(dir + "/conf.yml") if err != nil { cont, err = ioutil.ReadFile(dir + "/conf.sample.yml") } - logrus.Printf("cont is: \n%s", string(cont)) + Logf("cont is: \n%s", string(cont)) E2P(err) err = yaml.Unmarshal(cont, config) E2P(err) diff --git a/dtmcli/barrier.go b/dtmcli/barrier.go index 4a97a55..9147413 100644 --- a/dtmcli/barrier.go +++ b/dtmcli/barrier.go @@ -8,7 +8,6 @@ import ( "net/url" "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" ) @@ -79,7 +78,7 @@ func ThroughBarrierCall(db *sql.DB, transInfo *TransInfo, busiCall BusiFunc) (re return } defer func() { - logrus.Printf("result is %v error is %v", res, rerr) + common.Logf("result is %v error is %v", res, rerr) if x := recover(); x != nil { tx.Rollback() panic(x) @@ -96,7 +95,7 @@ func ThroughBarrierCall(db *sql.DB, transInfo *TransInfo, busiCall BusiFunc) (re }[ti.BranchType] originAffected, _ := insertBarrier(tx, ti.TransType, ti.Gid, ti.BranchID, originType, ti.BranchType) currentAffected, rerr := insertBarrier(tx, ti.TransType, ti.Gid, ti.BranchID, ti.BranchType, ti.BranchType) - logrus.Printf("originAffected: %d currentAffected: %d", originAffected, currentAffected) + common.Logf("originAffected: %d currentAffected: %d", originAffected, currentAffected) if (ti.BranchType == "cancel" || ti.BranchType == "compensate") && originAffected > 0 { // 这个是空补偿,返回成功 res = ResultSuccess return diff --git a/dtmcli/message.go b/dtmcli/message.go index 02df337..2605fda 100644 --- a/dtmcli/message.go +++ b/dtmcli/message.go @@ -1,7 +1,6 @@ package dtmcli import ( - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" ) @@ -40,7 +39,7 @@ func NewMsg(server string, gid string) *Msg { // Add add a new step func (s *Msg) Add(action string, postData interface{}) *Msg { - logrus.Printf("msg %s Add %s %v", s.MsgData.Gid, action, postData) + common.Logf("msg %s Add %s %v", s.MsgData.Gid, action, postData) step := MsgStep{ Action: action, Data: common.MustMarshalString(postData), diff --git a/dtmcli/saga.go b/dtmcli/saga.go index b32a340..b2312d5 100644 --- a/dtmcli/saga.go +++ b/dtmcli/saga.go @@ -1,7 +1,6 @@ package dtmcli import ( - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" ) @@ -40,7 +39,7 @@ func NewSaga(server string, gid string) *Saga { // Add add a saga step func (s *Saga) Add(action string, compensate string, postData interface{}) *Saga { - logrus.Printf("saga %s Add %s %s %v", s.SagaData.Gid, action, compensate, postData) + common.Logf("saga %s Add %s %s %v", s.SagaData.Gid, action, compensate, postData) step := SagaStep{ Action: action, Compensate: compensate, diff --git a/dtmsvr/cron.go b/dtmsvr/cron.go index a851918..1f6ec51 100644 --- a/dtmsvr/cron.go +++ b/dtmsvr/cron.go @@ -7,7 +7,6 @@ import ( "runtime/debug" "time" - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" ) @@ -55,7 +54,7 @@ func lockOneTrans(expireIn time.Duration) *TransGlobal { func handlePanic(perr *error) { if err := recover(); err != nil { - common.RedLogf("----panic %v handlered\n%s", err, string(debug.Stack())) + common.LogRedf("----panic %v handlered\n%s", err, string(debug.Stack())) if perr != nil { *perr = fmt.Errorf("dtm panic: %v", err) } @@ -65,6 +64,6 @@ func handlePanic(perr *error) { func sleepCronTime() { delta := math.Min(3, float64(config.TransCronInterval)) interval := time.Duration((float64(config.TransCronInterval) - rand.Float64()*delta) * float64(time.Second)) - logrus.Printf("sleeping for %v", interval) + common.Logf("sleeping for %v", interval) time.Sleep(interval) } diff --git a/dtmsvr/dtmsvr_test.go b/dtmsvr/dtmsvr_test.go index a898375..fb359c8 100644 --- a/dtmsvr/dtmsvr_test.go +++ b/dtmsvr/dtmsvr_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" @@ -103,7 +102,7 @@ func assertSucceed(t *testing.T, gid string) { } func genMsg(gid string) *dtmcli.Msg { - logrus.Printf("beginning a msg test ---------------- %s", gid) + common.Logf("beginning a msg test ---------------- %s", gid) msg := dtmcli.NewMsg(examples.DtmServer, gid) msg.QueryPrepared = examples.Busi + "/CanSubmit" req := examples.GenTransReq(30, false, false) @@ -113,7 +112,7 @@ func genMsg(gid string) *dtmcli.Msg { } func genSaga(gid string, outFailed bool, inFailed bool) *dtmcli.Saga { - logrus.Printf("beginning a saga test ---------------- %s", gid) + common.Logf("beginning a saga test ---------------- %s", gid) saga := dtmcli.NewSaga(examples.DtmServer, gid) req := examples.GenTransReq(30, outFailed, inFailed) saga.Add(examples.Busi+"/TransOut", examples.Busi+"/TransOutRevert", &req) @@ -153,7 +152,7 @@ func TestSqlDB(t *testing.T) { } db.Must().Exec("insert ignore into dtm_barrier.barrier(trans_type, gid, branch_id, branch_type, reason) values('saga', 'gid1', 'branch_id1', 'action', 'saga')") _, err := dtmcli.ThroughBarrierCall(db.ToSQLDB(), transInfo, func(db *sql.Tx) (interface{}, error) { - logrus.Printf("rollback gid2") + common.Logf("rollback gid2") return nil, fmt.Errorf("gid2 error") }) asserts.Error(err, fmt.Errorf("gid2 error")) @@ -163,14 +162,14 @@ func TestSqlDB(t *testing.T) { asserts.Equal(dbr.RowsAffected, int64(0)) gid2Res := common.M{"result": "first"} _, err = dtmcli.ThroughBarrierCall(db.ToSQLDB(), transInfo, func(db *sql.Tx) (interface{}, error) { - logrus.Printf("submit gid2") + common.Logf("submit gid2") return gid2Res, nil }) asserts.Nil(err) dbr = db.Model(&dtmcli.BarrierModel{}).Where("gid=?", "gid2").Find(&[]dtmcli.BarrierModel{}) asserts.Equal(dbr.RowsAffected, int64(1)) newResult, err := dtmcli.ThroughBarrierCall(db.ToSQLDB(), transInfo, func(db *sql.Tx) (interface{}, error) { - logrus.Printf("submit gid2") + common.Logf("submit gid2") return common.MS{"result": "ignored"}, nil }) asserts.Equal(newResult, gid2Res) diff --git a/dtmsvr/main.go b/dtmsvr/main.go index 5061beb..2b8ccf0 100644 --- a/dtmsvr/main.go +++ b/dtmsvr/main.go @@ -4,7 +4,6 @@ import ( "fmt" "time" - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" "github.com/yedf/dtm/examples" ) @@ -13,10 +12,10 @@ var dtmsvrPort = 8080 // StartSvr StartSvr func StartSvr() { - logrus.Printf("start dtmsvr") + common.Logf("start dtmsvr") app := common.GetGinApp() addRoute(app) - logrus.Printf("dtmsvr listen at: %d", dtmsvrPort) + common.Logf("dtmsvr listen at: %d", dtmsvrPort) go app.Run(fmt.Sprintf(":%d", dtmsvrPort)) time.Sleep(100 * time.Millisecond) } diff --git a/dtmsvr/trans.go b/dtmsvr/trans.go index d2166a5..ccc03e9 100644 --- a/dtmsvr/trans.go +++ b/dtmsvr/trans.go @@ -5,7 +5,6 @@ import ( "time" "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" "gorm.io/gorm" @@ -131,12 +130,12 @@ func (t *TransGlobal) processInner(db *common.DB) (rerr error) { defer handlePanic(&rerr) defer func() { if TransProcessedTestChan != nil { - logrus.Printf("processed: %s", t.Gid) + common.Logf("processed: %s", t.Gid) TransProcessedTestChan <- t.Gid - logrus.Printf("notified: %s", t.Gid) + common.Logf("notified: %s", t.Gid) } }() - logrus.Printf("processing: %s status: %s", t.Gid, t.Status) + common.Logf("processing: %s status: %s", t.Gid, t.Status) if t.Status == "prepared" && t.TransType != "msg" { t.changeStatus(db, "aborting") } @@ -195,7 +194,7 @@ func TransFromContext(c *gin.Context) *TransGlobal { b, err := c.GetRawData() e2p(err) common.MustUnmarshal(b, &data) - logrus.Printf("creating trans in prepare") + common.Logf("creating trans in prepare") if data["steps"] != nil { data["data"] = common.MustMarshalString(data["steps"]) } diff --git a/dtmsvr/trans_saga_barrier_test.go b/dtmsvr/trans_saga_barrier_test.go index 1749c4e..52c189d 100644 --- a/dtmsvr/trans_saga_barrier_test.go +++ b/dtmsvr/trans_saga_barrier_test.go @@ -3,8 +3,8 @@ package dtmsvr import ( "testing" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" "github.com/yedf/dtm/examples" ) @@ -20,7 +20,7 @@ func sagaBarrierNormal(t *testing.T) { saga := dtmcli.NewSaga(DtmServer, "sagaBarrierNormal"). Add(Busi+"/SagaBTransOut", Busi+"/SagaBTransOutCompensate", req). Add(Busi+"/SagaBTransIn", Busi+"/SagaBTransInCompensate", req) - logrus.Printf("busi trans submit") + common.Logf("busi trans submit") err := saga.Submit() e2p(err) WaitTransProcessed(saga.Gid) @@ -31,7 +31,7 @@ func sagaBarrierRollback(t *testing.T) { saga := dtmcli.NewSaga(DtmServer, "sagaBarrierRollback"). Add(Busi+"/SagaBTransOut", Busi+"/SagaBTransOutCompensate", &examples.TransReq{Amount: 30}). Add(Busi+"/SagaBTransIn", Busi+"/SagaBTransInCompensate", &examples.TransReq{Amount: 30, TransInResult: "FAILURE"}) - logrus.Printf("busi trans submit") + common.Logf("busi trans submit") err := saga.Submit() e2p(err) WaitTransProcessed(saga.Gid) diff --git a/dtmsvr/trans_tcc_barrier_test.go b/dtmsvr/trans_tcc_barrier_test.go index e1eb9d5..b0e4620 100644 --- a/dtmsvr/trans_tcc_barrier_test.go +++ b/dtmsvr/trans_tcc_barrier_test.go @@ -8,7 +8,6 @@ import ( "github.com/gin-gonic/gin" "github.com/go-resty/resty/v2" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" @@ -62,7 +61,7 @@ func tccBarrierDisorder(t *testing.T) { res, err := examples.TccBarrierTransOutCancel(c) if !sleeped { sleeped = true - logrus.Printf("sleep before cancel return") + common.Logf("sleep before cancel return") <-timeoutChan finishedChan <- "1" } @@ -81,7 +80,7 @@ func tccBarrierDisorder(t *testing.T) { }, "registerTccBranch") assert.Nil(t, err) go func() { - logrus.Printf("sleeping to wait for tcc try timeout") + common.Logf("sleeping to wait for tcc try timeout") <-timeoutChan r, _ := common.RestyClient.R(). SetBody(body). @@ -96,10 +95,10 @@ func tccBarrierDisorder(t *testing.T) { assert.True(t, strings.Contains(r.String(), "FAILURE")) finishedChan <- "1" }() - logrus.Printf("cron to timeout and then call cancel") + common.Logf("cron to timeout and then call cancel") go CronTransOnce(60 * time.Second) time.Sleep(100 * time.Millisecond) - logrus.Printf("cron to timeout and then call cancelled twice") + common.Logf("cron to timeout and then call cancelled twice") CronTransOnce(60 * time.Second) timeoutChan <- "wake" timeoutChan <- "wake" diff --git a/dtmsvr/utils.go b/dtmsvr/utils.go index 0e6ea8b..45176c0 100644 --- a/dtmsvr/utils.go +++ b/dtmsvr/utils.go @@ -38,13 +38,13 @@ var TransProcessedTestChan chan string = nil // WaitTransProcessed only for test usage. wait for transaction processed once func WaitTransProcessed(gid string) { - logrus.Printf("waiting for gid %s", gid) + common.Logf("waiting for gid %s", gid) id := <-TransProcessedTestChan for id != gid { logrus.Errorf("-------id %s not match gid %s", id, gid) id = <-TransProcessedTestChan } - logrus.Printf("finish for gid %s", gid) + common.Logf("finish for gid %s", gid) } var gNode *snowflake.Node = nil diff --git a/examples/main_base.go b/examples/main_base.go index 3291b93..310e5bc 100644 --- a/examples/main_base.go +++ b/examples/main_base.go @@ -5,7 +5,6 @@ import ( "time" "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" ) @@ -21,10 +20,10 @@ var Busi string = fmt.Sprintf("http://localhost:%d%s", BusiPort, BusiAPI) // BaseAppStartup base app startup func BaseAppStartup() *gin.Engine { - logrus.Printf("examples starting") + common.Logf("examples starting") app := common.GetGinApp() BaseAddRoute(app) - logrus.Printf("Starting busi at: %d", BusiPort) + common.Logf("Starting busi at: %d", BusiPort) go app.Run(fmt.Sprintf(":%d", BusiPort)) time.Sleep(100 * time.Millisecond) return app @@ -63,7 +62,7 @@ var MainSwitch mainSwitchType func handleGeneralBusiness(c *gin.Context, result1 string, result2 string, busi string) (interface{}, error) { info := infoFromContext(c) res := common.OrString(result1, result2, "SUCCESS") - logrus.Printf("%s %s result: %s", busi, info.String(), res) + common.Logf("%s %s result: %s", busi, info.String(), res) return M{"dtm_result": res}, nil } @@ -89,7 +88,7 @@ func BaseAddRoute(app *gin.Engine) { return handleGeneralBusiness(c, MainSwitch.TransOutRevertResult.Fetch(), "", "TransOutRevert") })) app.GET(BusiAPI+"/CanSubmit", common.WrapHandler(func(c *gin.Context) (interface{}, error) { - logrus.Printf("%s CanSubmit", c.Query("gid")) + common.Logf("%s CanSubmit", c.Query("gid")) return common.OrString(MainSwitch.CanSubmitResult.Fetch(), "SUCCESS"), nil })) } diff --git a/examples/main_msg.go b/examples/main_msg.go index 16e9a62..19bb057 100644 --- a/examples/main_msg.go +++ b/examples/main_msg.go @@ -2,7 +2,7 @@ package examples import ( "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" + "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" ) @@ -12,14 +12,14 @@ func MsgSetup(app *gin.Engine) { // MsgFireRequest 1 func MsgFireRequest() string { - logrus.Printf("a busi transaction begin") + common.Logf("a busi transaction begin") req := &TransReq{Amount: 30} msg := dtmcli.NewMsg(DtmServer, dtmcli.MustGenGid(DtmServer)). Add(Busi+"/TransOut", req). Add(Busi+"/TransIn", req) err := msg.Prepare(Busi + "/TransQuery") e2p(err) - logrus.Printf("busi trans submit") + common.Logf("busi trans submit") err = msg.Submit() e2p(err) return msg.Gid diff --git a/examples/main_saga.go b/examples/main_saga.go index 36c349e..c66faa5 100644 --- a/examples/main_saga.go +++ b/examples/main_saga.go @@ -2,7 +2,7 @@ package examples import ( "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" + "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" ) @@ -12,7 +12,7 @@ func SagaSetup(app *gin.Engine) { // SagaFireRequest 1 func SagaFireRequest() string { - logrus.Printf("a saga busi transaction begin") + common.Logf("a saga busi transaction begin") req := &TransReq{ Amount: 30, TransInResult: "SUCCESS", @@ -21,9 +21,9 @@ func SagaFireRequest() string { saga := dtmcli.NewSaga(DtmServer, dtmcli.MustGenGid(DtmServer)). Add(Busi+"/TransOut", Busi+"/TransOutRevert", req). Add(Busi+"/TransIn", Busi+"/TransInRevert", req) - logrus.Printf("saga busi trans submit") + common.Logf("saga busi trans submit") err := saga.Submit() - logrus.Printf("result gid is: %s", saga.Gid) + common.Logf("result gid is: %s", saga.Gid) e2p(err) return saga.Gid } diff --git a/examples/main_saga_barrier.go b/examples/main_saga_barrier.go index 9c50fa8..5d2755c 100644 --- a/examples/main_saga_barrier.go +++ b/examples/main_saga_barrier.go @@ -4,19 +4,18 @@ import ( "database/sql" "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" ) // SagaBarrierFireRequest 1 func SagaBarrierFireRequest() string { - logrus.Printf("a busi transaction begin") + common.Logf("a busi transaction begin") req := &TransReq{Amount: 30} saga := dtmcli.NewSaga(DtmServer, dtmcli.MustGenGid(DtmServer)). Add(Busi+"/SagaBTransOut", Busi+"/SagaBTransOutCompensate", req). Add(Busi+"/SagaBTransIn", Busi+"/SagaBTransInCompensate", req) - logrus.Printf("busi trans submit") + common.Logf("busi trans submit") err := saga.Submit() e2p(err) return saga.Gid @@ -28,7 +27,7 @@ func SagaBarrierAddRoute(app *gin.Engine) { app.POST(BusiAPI+"/SagaBTransInCompensate", common.WrapHandler(sagaBarrierTransInCompensate)) app.POST(BusiAPI+"/SagaBTransOut", common.WrapHandler(sagaBarrierTransOut)) app.POST(BusiAPI+"/SagaBTransOutCompensate", common.WrapHandler(sagaBarrierTransOutCompensate)) - logrus.Printf("examples listening at %d", BusiPort) + common.Logf("examples listening at %d", BusiPort) } func sagaBarrierAdjustBalance(sdb *sql.Tx, uid int, amount int) (interface{}, error) { diff --git a/examples/main_tcc.go b/examples/main_tcc.go index 7e483bd..44279b0 100644 --- a/examples/main_tcc.go +++ b/examples/main_tcc.go @@ -3,7 +3,6 @@ package examples import ( "github.com/gin-gonic/gin" "github.com/go-resty/resty/v2" - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" ) @@ -13,7 +12,7 @@ func TccSetup(app *gin.Engine) { app.POST(BusiAPI+"/TransInTccParent", common.WrapHandler(func(c *gin.Context) (interface{}, error) { tcc, err := dtmcli.TccFromReq(c) e2p(err) - logrus.Printf("TransInTccParent ") + common.Logf("TransInTccParent ") return tcc.CallBranch(&TransReq{Amount: reqFrom(c).Amount}, Busi+"/TransIn", Busi+"/TransInConfirm", Busi+"/TransInRevert") })) } @@ -34,7 +33,7 @@ func TccFireRequestNested() string { // TccFireRequest 1 func TccFireRequest() string { - logrus.Printf("tcc simple transaction begin") + common.Logf("tcc simple transaction begin") gid := dtmcli.MustGenGid(DtmServer) err := dtmcli.TccGlobalTransaction(DtmServer, gid, func(tcc *dtmcli.Tcc) (*resty.Response, error) { resp, err := tcc.CallBranch(&TransReq{Amount: 30}, Busi+"/TransOut", Busi+"/TransOutConfirm", Busi+"/TransOutRevert") diff --git a/examples/main_tcc_barrier.go b/examples/main_tcc_barrier.go index 1f06d6e..f1464ff 100644 --- a/examples/main_tcc_barrier.go +++ b/examples/main_tcc_barrier.go @@ -6,14 +6,13 @@ import ( "github.com/gin-gonic/gin" "github.com/go-resty/resty/v2" - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" ) // TccBarrierFireRequest 1 func TccBarrierFireRequest() string { - logrus.Printf("tcc transaction begin") + common.Logf("tcc transaction begin") gid := dtmcli.MustGenGid(DtmServer) err := dtmcli.TccGlobalTransaction(DtmServer, gid, func(tcc *dtmcli.Tcc) (*resty.Response, error) { resp, err := tcc.CallBranch(&TransReq{Amount: 30}, Busi+"/TccBTransOutTry", Busi+"/TccBTransOutConfirm", Busi+"/TccBTransOutCancel") @@ -34,7 +33,7 @@ func TccBarrierAddRoute(app *gin.Engine) { app.POST(BusiAPI+"/TccBTransOutTry", common.WrapHandler(tccBarrierTransOutTry)) app.POST(BusiAPI+"/TccBTransOutConfirm", common.WrapHandler(tccBarrierTransOutConfirm)) app.POST(BusiAPI+"/TccBTransOutCancel", common.WrapHandler(TccBarrierTransOutCancel)) - logrus.Printf("examples listening at %d", BusiPort) + common.Logf("examples listening at %d", BusiPort) } const transInUID = 1 diff --git a/examples/quick_start.go b/examples/quick_start.go index 409e0b3..b42ef2c 100644 --- a/examples/quick_start.go +++ b/examples/quick_start.go @@ -5,7 +5,6 @@ import ( "time" "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" ) @@ -22,7 +21,7 @@ var qsBusi = fmt.Sprintf("http://localhost:%d%s", qsBusiPort, qsBusiAPI) func QsStartSvr() { app := common.GetGinApp() qsAddRoute(app) - logrus.Printf("quick qs examples listening at %d", qsBusiPort) + common.Logf("quick qs examples listening at %d", qsBusiPort) go app.Run(fmt.Sprintf(":%d", qsBusiPort)) time.Sleep(100 * time.Millisecond) }