diff --git a/candle.go b/candle.go index 688c614..3a75fcb 100644 --- a/candle.go +++ b/candle.go @@ -212,10 +212,10 @@ func (candle *Candle) PushToWriteLogChan(cr *Core) error { did := candle.InstID + candle.Period + candle.Data[0].(string) candle.Id = HashString(did) ncd, _ := candle.ToStruct(cr) - fmt.Println("ncd: ", ncd) + logrus.Debug("ncd: ", ncd) cd, err := json.Marshal(ncd) if err != nil { - fmt.Println("PushToWriteLog json marshal candle err: ", err) + logrus.Error("PushToWriteLog json marshal candle err: ", err) } candle = ncd wg := WriteLog{ @@ -257,38 +257,38 @@ func (cl *Candle) ToStruct(core *Core) (*Candle, error) { // 将字符串转换为 int64 类型的时间戳 ts, err := strconv.ParseInt(cl.Data[0].(string), 10, 64) if err != nil { - fmt.Println("Error parsing timestamp:", err) + logrus.Error("Error parsing timestamp:", err) return nil, err } ncd.Timestamp = time.Unix(ts/1000, (ts%1000)*1000000) // 纳秒级别 op, err := strconv.ParseFloat(cl.Data[1].(string), 64) if err != nil { - fmt.Println("Error parsing string to float64:", err) + logrus.Error("Error parsing string to float64:", err) return nil, err } ncd.Open = op hi, err := strconv.ParseFloat(cl.Data[2].(string), 64) if err != nil { - fmt.Println("Error parsing string to float64:", err) + logrus.Error("Error parsing string to float64:", err) return nil, err } ncd.High = hi lo, err := strconv.ParseFloat(cl.Data[3].(string), 64) if err != nil { - fmt.Println("Error parsing string to float64:", err) + logrus.Error("Error parsing string to float64:", err) return nil, err } ncd.Low = lo clse, err := strconv.ParseFloat(cl.Data[4].(string), 64) if err != nil { - fmt.Println("Error parsing string to float64:", err) + logrus.Error("Error parsing string to float64:", err) return nil, err } ncd.Close = clse ncd.VolCcy, err = strconv.ParseFloat(cl.Data[6].(string), 64) if err != nil { - fmt.Println("Error parsing string to float64:", err) + logrus.Error("Error parsing string to float64:", err) return nil, err } if cl.Data[6].(string) == "1" { @@ -307,7 +307,7 @@ func (core *Core) SaveUniKey(period string, keyName string, extt time.Duration, core.RedisLocalCli.Expire(refName, extt) // 为保证唯一性机制,防止SaveToSortSet 被重复执行 if len(refRes) != 0 { - fmt.Println("refName exist: ", refName) + logrus.Error("refName exist: ", refName) return } @@ -324,9 +324,9 @@ func (core *Core) SaveToSortSet(period string, keyName string, extt time.Duratio } rs, err := core.RedisLocalCli.ZAdd(setName, z).Result() if err != nil { - fmt.Println("err of ma7|ma30 add to redis:", err) + logrus.Error("err of ma7|ma30 add to redis:", err) } else { - fmt.Println("sortedSet added to redis:", rs, keyName) + logrus.Info("sortedSet added to redis:", rs, keyName) } } @@ -441,11 +441,11 @@ func (core *Core) GetRangeKeyList(pattern string, from time.Time) ([]*simple.Jso nv := pattern + strconv.FormatInt(v, 10) str, err := redisCli.Get(nv).Result() if err != nil { - fmt.Println("err of redis get key:", nv, err) + logrus.Error("err of redis get key:", nv, err) } cur, err := simple.NewJson([]byte(str)) if err != nil { - fmt.Println("err of create newJson:", str, err) + logrus.Error("err of create newJson:", str, err) } res = append(res, cur) } @@ -465,12 +465,12 @@ func (cl *Candle) SetToKey(core *Core) ([]interface{}, error) { cl.Timestamp = tm dt, err := json.Marshal(cl) if err != nil { - fmt.Println("candle Save to String err:", err) + logrus.Error("candle Save to String err:", err) } - fmt.Println("candle Save to String: ", string(dt)) + logrus.Info("candle Save to String: ", string(dt)) exp, err := core.PeriodToMinutes(cl.Period) if err != nil { - fmt.Println("err of PeriodToMinutes:", err) + logrus.Error("err of PeriodToMinutes:", err) } // expf := float64(exp) * 60 expf := utils.Sqrt(float64(exp)) * 100 @@ -478,19 +478,20 @@ func (cl *Candle) SetToKey(core *Core) ([]interface{}, error) { curVolstr, _ := data[5].(string) curVol, err := strconv.ParseFloat(curVolstr, 64) if err != nil { - fmt.Println("err of convert ts:", err) + logrus.Error("err of convert ts:", err) } curVolCcystr, _ := data[6].(string) curVolCcy, err := strconv.ParseFloat(curVolCcystr, 64) curPrice := curVolCcy / curVol if curPrice <= 0 { - fmt.Println("price有问题", curPrice, "dt: ", string(dt), "from:", cl.From) + logrus.Error("price有问题", curPrice, "dt: ", string(dt), "from:", cl.From) err = errors.New("price有问题") return cl.Data, err } redisCli := core.RedisLocalCli // tm := time.UnixMilli(tsi).Format("2006-01-02 15:04") - fmt.Println("setToKey:", keyName, "ts: ", "price: ", curPrice, "from:", cl.From) + fmt.Println() + logrus.Info("setToKey:", keyName, "ts: ", "price: ", curPrice, "from:", cl.From) redisCli.Set(keyName, dt, extt).Result() core.SaveUniKey(cl.Period, keyName, extt, tsi) return cl.Data, err diff --git a/config.go b/config.go index 635be2a..3f2a2ae 100644 --- a/config.go +++ b/config.go @@ -8,6 +8,7 @@ import ( "strings" simple "github.com/bitly/go-simplejson" + logrus "github.com/sirupsen/logrus" ) type MyConfig struct { @@ -58,12 +59,12 @@ func (cfg MyConfig) Init() (MyConfig, error) { if err != nil { jsonStr, err = ioutil.ReadFile("configs/basicConfig.json") if err != nil { - fmt.Println("err2:", err.Error()) + logrus.Error("err2:", err.Error()) return cfg, err } cfg.Config, err = simple.NewJson([]byte(jsonStr)) if err != nil { - fmt.Println("err2:", err.Error()) + logrus.Error("err2:", err.Error()) return cfg, err } cfg.Env = env @@ -96,7 +97,7 @@ func (cfg MyConfig) Init() (MyConfig, error) { func (cfg *MyConfig) GetConfigJson(arr []string) *simple.Json { env := os.Getenv("GO_ENV") - fmt.Println("env: ", env) + logrus.Info("env: ", env) cfg.Env = env json, err := ioutil.ReadFile("/go/json/basicConfig.json") @@ -108,11 +109,11 @@ func (cfg *MyConfig) GetConfigJson(arr []string) *simple.Json { } } if err != nil { - fmt.Println("read file err: ", err) + logrus.Error("read file err: ", err) } rjson, err := simple.NewJson(json) if err != nil { - fmt.Println("newJson err: ", err) + logrus.Error("newJson err: ", err) } for _, s := range arr { rjson = rjson.Get(s) diff --git a/core.go b/core.go index 18a8a9c..36a4a8a 100644 --- a/core.go +++ b/core.go @@ -71,7 +71,7 @@ type SubAction struct { } func (rst *RestQueue) Show(cr *Core) { - fmt.Println("restQueue:", rst.InstId, rst.Bar, rst.Limit) + logrus.Info("restQueue:", rst.InstId, rst.Bar, rst.Limit) } func (rst *RestQueue) Save(cr *Core) { @@ -89,12 +89,12 @@ func (rst *RestQueue) Save(cr *Core) { } link := "/api/v5/market/candles?instId=" + rst.InstId + "&bar=" + rst.Bar + limitSec + afterSec + beforeSec - fmt.Println("restLink: ", link) + logrus.Info("restLink: ", link) rsp, err := cr.v5PublicInvoke(link) if err != nil { - fmt.Println("cr.v5PublicInvoke err:", err) + logrus.Info("cr.v5PublicInvoke err:", err) } else { - fmt.Println("cr.v5PublicInvoke result count:", len(rsp.Data)) + logrus.Info("cr.v5PublicInvoke result count:", len(rsp.Data)) } cr.SaveCandle(rst.InstId, rst.Bar, rsp, rst.Duration, rst.WithWs) } @@ -103,7 +103,7 @@ func WriteLogProcess(cr *Core) { for { wg := <-cr.WriteLogChan go func(wg *WriteLog) { - fmt.Println("start writelog: " + wg.Tag + " " + wg.Id) + logrus.Info("start writelog: " + wg.Tag + " " + wg.Id) wg.Process(cr) }(wg) time.Sleep(50 * time.Millisecond) @@ -120,9 +120,9 @@ func (core *Core) Init() { gitBranch := os.Getenv("gitBranchName") commitID := os.Getenv("gitCommitID") - fmt.Println("当前环境: ", core.Env) - fmt.Println("gitBranch: ", gitBranch) - fmt.Println("gitCommitID: ", commitID) + logrus.Info("当前环境: ", core.Env) + logrus.Info("gitBranch: ", gitBranch) + logrus.Info("gitCommitID: ", commitID) cfg := MyConfig{} cfg, _ = cfg.Init() core.Cfg = &cfg @@ -133,7 +133,7 @@ func (core *Core) Init() { // 跟订单有关的都关掉 // core.OrderChan = make(chan *private.Order) if err != nil { - fmt.Println("init redis client err: ", err) + logrus.Error("init redis client err: ", err) } } @@ -147,7 +147,7 @@ func (core *Core) GetRedisCliFromConf(conf RedisConfig) (*redis.Client, error) { if pong == "PONG" && err == nil { return client, err } else { - fmt.Println("redis状态不可用:", conf.Url, conf.Password, conf.Index, err) + logrus.Error("redis状态不可用:", conf.Url, conf.Password, conf.Index, err) } return client, nil @@ -170,7 +170,7 @@ func (core *Core) GetRemoteRedisLocalCli() (*redis.Client, error) { if pong == "PONG" && err == nil { return client, err } else { - fmt.Println("redis状态不可用:", ru, rp, ri, err) + logrus.Error("redis状态不可用:", ru, rp, ri, err) } return client, nil @@ -192,7 +192,7 @@ func (core *Core) GetRedisLocalCli() (*redis.Client, error) { if pong == "PONG" && err == nil { return client, err } else { - fmt.Println("redis状态不可用:", ru, rp, ri, err) + logrus.Error("redis状态不可用:", ru, rp, ri, err) } return client, nil @@ -308,7 +308,7 @@ func (core *Core) RestInvoke(subUrl string, method string) (*rest.RESTAPIResult, rest.SetSimulate(isDemo).SetAPIKey(key, secure, pass) response, err := rest.Run(context.Background()) if err != nil { - fmt.Println("restInvoke1 err:", subUrl, err) + logrus.Error("restInvoke1 err:", subUrl, err) } return response, err } @@ -404,7 +404,7 @@ func (core *Core) GetScoreList(count int) []string { // redisCli := core.RedisLocalCli myFocusList := core.Cfg.Config.Get("focusList").MustArray() - fmt.Println("curList: ", myFocusList) + logrus.Debug("curList: ", myFocusList) lst := []string{} for _, v := range myFocusList { lst = append(lst, v.(string)) @@ -535,7 +535,7 @@ func (cr *Core) ProcessOrder(od *private.Order) error { // TODO FIXME cli2 res, _ := cr.RedisLocalCli.Publish(cn, string(bj)).Result() - fmt.Println("order publish res: ", res, " content: ", string(bj)) + logrus.Info("order publish res: ", res, " content: ", string(bj)) rsch := ORDER_RESP_PUBLISH + suffix bj1, _ := json.Marshal(res) @@ -585,9 +585,9 @@ func (cr *Core) AddToGeneralCandleChnl(candle *Candle, channels []string) { redisCli := cr.RedisLocalCli ab, err := json.Marshal(candle) if err != nil { - fmt.Println("candle marshal err: ", err) + logrus.Error("candle marshal err: ", err) } - fmt.Println("ab: ", string(ab)) + logrus.Debug("ab: ", string(ab)) for _, v := range channels { suffix := "" env := os.Getenv("GO_ENV") @@ -597,7 +597,7 @@ func (cr *Core) AddToGeneralCandleChnl(candle *Candle, channels []string) { vd := v + suffix _, err := redisCli.Publish(vd, string(ab)).Result() if err != nil { - fmt.Println("err of ma7|ma30 add to redis2:", err, candle.From) + logrus.Error("err of ma7|ma30 add to redis2:", err, candle.From) } } } diff --git a/maX.go b/maX.go index c8b1f14..97d3fc4 100644 --- a/maX.go +++ b/maX.go @@ -51,25 +51,25 @@ func (mx MaX) SetToKey(cr *Core) ([]interface{}, error) { dj, err := json.Marshal(mx) if err != nil { - fmt.Println("maX SetToKey json marshal err: ", err) + logrus.Error("maX SetToKey json marshal err: ", err) } extt, err := cr.GetExpiration(mx.Period) if err != nil { - fmt.Println("max SetToKey err: ", err) + logrus.Error("max SetToKey err: ", err) return mx.Data, err } // fmt.Println(utils.GetFuncName(), " step2 ", mx.InstID, " ", mx.Period) // tm := time.UnixMilli(mx.Ts).Format("01-02 15:04") cli := cr.RedisLocalCli if len(string(dj)) == 0 { - fmt.Println("mx data is block data: ", mx, string(dj)) + logrus.Error("mx data is block data: ", mx, string(dj)) err := errors.New("data is block") return mx.Data, err } // fmt.Println(utils.GetFuncName(), " step3 ", mx.InstID, " ", mx.Period) _, err = cli.Set(keyName, dj, extt).Result() if err != nil { - fmt.Println(GetFuncName(), " maXSetToKey err:", err) + logrus.Error(GetFuncName(), " maXSetToKey err:", err) return mx.Data, err } // fmt.Println(utils.GetFuncName(), " step4 ", mx.InstID, " ", mx.Period) @@ -85,7 +85,7 @@ func Int64ToTime(ts int64) (time.Time, error) { // 获取东八区(北京时间)的时区信息 loc, err := time.LoadLocation("Asia/Shanghai") if err != nil { - fmt.Println("加载时区失败:", err) + logrus.Error("加载时区失败:", err) return t, err } // 将时间转换为东八区时间 @@ -95,7 +95,7 @@ func Int64ToTime(ts int64) (time.Time, error) { func (mx *MaX) PushToWriteLogChan(cr *Core) error { s := strconv.FormatFloat(float64(mx.Ts), 'f', 0, 64) did := mx.InstID + mx.Period + s - fmt.Println("did of max:", did) + logrus.Error("did of max:", did) mx0 := MaX{} mx0.InstID = mx.InstID mx0.Period = mx.Period diff --git a/util.go b/util.go index 2ecfea8..41b02d9 100644 --- a/util.go +++ b/util.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + logrus "github.com/sirupsen/logrus" "math" "math/rand" "runtime" @@ -103,7 +104,7 @@ func JsonToMap(str string) (map[string]interface{}, error) { err := json.Unmarshal([]byte(str), &tempMap) if err != nil { - fmt.Println("Unmarshal err: ", err, str) + logrus.Error("Unmarshal err: ", err, str) } return tempMap, err