From 87e5019b08af6f4e01511b0f779e0f7e849a0e09 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 7 Aug 2020 09:15:50 +0800 Subject: [PATCH] improve error logging --- bbgo/exchange/binance/stream.go | 14 ++++++++------ slack/logrus_look.go | 23 ++++++++++++++--------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/bbgo/exchange/binance/stream.go b/bbgo/exchange/binance/stream.go index 77205f79e..4603f7244 100644 --- a/bbgo/exchange/binance/stream.go +++ b/bbgo/exchange/binance/stream.go @@ -3,6 +3,7 @@ package binance import ( "context" "github.com/c9s/bbgo/pkg/util" + "strings" "time" "github.com/adshao/go-binance" @@ -23,9 +24,9 @@ type StreamRequest struct { type PrivateStream struct { types.StandardPrivateStream - Client *binance.Client - ListenKey string - Conn *websocket.Conn + Client *binance.Client + ListenKey string + Conn *websocket.Conn connectCallbacks []func(stream *PrivateStream) @@ -38,7 +39,6 @@ type PrivateStream struct { executionReportEventCallbacks []func(event *ExecutionReportEvent) } - func (s *PrivateStream) dial(listenKey string) (*websocket.Conn, error) { url := "wss://stream.binance.com:9443/ws/" + listenKey conn, _, err := websocket.DefaultDialer.Dial(url, nil) @@ -105,13 +105,15 @@ func (s *PrivateStream) read(ctx context.Context, eventC chan interface{}) { return case <-ticker.C: - if err := s.Conn.WriteControl(websocket.PingMessage, []byte("hb"), time.Now().Add(1 * time.Second)) ; err != nil { + if err := s.Conn.WriteControl(websocket.PingMessage, []byte("hb"), time.Now().Add(1*time.Second)); err != nil { log.WithError(err).Error("ping error", err) } err := s.Client.NewKeepaliveUserStreamService().ListenKey(s.ListenKey).Do(ctx) if err != nil { - log.WithError(err).Error("listen key keep-alive error", err) + maskKey := s.ListenKey[0:5] + maskKey = maskKey + strings.Repeat("*", len(s.ListenKey)-1-5) + log.WithError(err).Errorf("listen key keep-alive error: %v key: %s", err, maskKey) } default: diff --git a/slack/logrus_look.go b/slack/logrus_look.go index cb2304962..48e763b6e 100644 --- a/slack/logrus_look.go +++ b/slack/logrus_look.go @@ -2,8 +2,10 @@ package slack import ( "context" + "fmt" "github.com/sirupsen/logrus" "github.com/slack-go/slack" + "strings" ) type SlackLogHook struct { @@ -36,19 +38,22 @@ func (t *SlackLogHook) Fire(e *logrus.Entry) error { var slackAttachments []slack.Attachment = nil - logerr, ok := e.Data["err"] - if ok { - slackAttachments = append(slackAttachments, slack.Attachment{ - Color: color, - Title: "Error", - Fields: []slack.AttachmentField{ - {Title: "Error", Value: logerr.(error).Error()}, - }, + // error fields + var fields []slack.AttachmentField + for k, d := range e.Data { + fields = append(fields, slack.AttachmentField{ + Title: k, Value: fmt.Sprintf("%v", d), }) } + slackAttachments = append(slackAttachments, slack.Attachment{ + Color: color, + Title: strings.ToUpper(e.Level.String()), + Fields: fields, + }) + _, _, err := t.Slack.PostMessageContext(context.Background(), t.ErrorChannel, - slack.MsgOptionText(e.Message, true), + slack.MsgOptionText(":balloon: "+e.Message, true), slack.MsgOptionAttachments(slackAttachments...)) return err