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 ( import (
"context" "context"
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
"strings"
"time" "time"
"github.com/adshao/go-binance" "github.com/adshao/go-binance"
@ -38,7 +39,6 @@ type PrivateStream struct {
executionReportEventCallbacks []func(event *ExecutionReportEvent) executionReportEventCallbacks []func(event *ExecutionReportEvent)
} }
func (s *PrivateStream) dial(listenKey string) (*websocket.Conn, error) { func (s *PrivateStream) dial(listenKey string) (*websocket.Conn, error) {
url := "wss://stream.binance.com:9443/ws/" + listenKey url := "wss://stream.binance.com:9443/ws/" + listenKey
conn, _, err := websocket.DefaultDialer.Dial(url, nil) 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) err := s.Client.NewKeepaliveUserStreamService().ListenKey(s.ListenKey).Do(ctx)
if err != nil { 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: default:

View File

@ -2,8 +2,10 @@ package slack
import ( import (
"context" "context"
"fmt"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/slack-go/slack" "github.com/slack-go/slack"
"strings"
) )
type SlackLogHook struct { type SlackLogHook struct {
@ -36,19 +38,22 @@ func (t *SlackLogHook) Fire(e *logrus.Entry) error {
var slackAttachments []slack.Attachment = nil var slackAttachments []slack.Attachment = nil
logerr, ok := e.Data["err"] // error fields
if ok { var fields []slack.AttachmentField
slackAttachments = append(slackAttachments, slack.Attachment{ for k, d := range e.Data {
Color: color, fields = append(fields, slack.AttachmentField{
Title: "Error", Title: k, Value: fmt.Sprintf("%v", d),
Fields: []slack.AttachmentField{
{Title: "Error", Value: logerr.(error).Error()},
},
}) })
} }
slackAttachments = append(slackAttachments, slack.Attachment{
Color: color,
Title: strings.ToUpper(e.Level.String()),
Fields: fields,
})
_, _, err := t.Slack.PostMessageContext(context.Background(), t.ErrorChannel, _, _, err := t.Slack.PostMessageContext(context.Background(), t.ErrorChannel,
slack.MsgOptionText(e.Message, true), slack.MsgOptionText(":balloon: "+e.Message, true),
slack.MsgOptionAttachments(slackAttachments...)) slack.MsgOptionAttachments(slackAttachments...))
return err return err