improve error logging

This commit is contained in:
c9s 2020-08-07 09:15:50 +08:00
parent 6b478b4e87
commit 87e5019b08
2 changed files with 22 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package binance
import (
"context"
"github.com/c9s/bbgo/pkg/util"
"strings"
"time"
"github.com/adshao/go-binance"
@ -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)
@ -111,7 +111,9 @@ func (s *PrivateStream) read(ctx context.Context, eventC chan interface{}) {
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:

View File

@ -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