add fields to slack notifier logs

This commit is contained in:
c9s 2020-10-28 17:34:12 +08:00
parent b22e0370b3
commit c96845ff6a

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/slack-go/slack" "github.com/slack-go/slack"
) )
@ -69,13 +69,15 @@ func (n *Notifier) NotifyTo(channel, format string, args ...interface{}) {
nonSlackArgs = args[:slackArgsOffset] nonSlackArgs = args[:slackArgsOffset]
} }
logrus.Infof(format, nonSlackArgs...) log.Infof(format, nonSlackArgs...)
_, _, err := n.client.PostMessageContext(context.Background(), channel, _, _, err := n.client.PostMessageContext(context.Background(), channel,
slack.MsgOptionText(fmt.Sprintf(format, nonSlackArgs...), true), slack.MsgOptionText(fmt.Sprintf(format, nonSlackArgs...), true),
slack.MsgOptionAttachments(slackAttachments...)) slack.MsgOptionAttachments(slackAttachments...))
if err != nil { if err != nil {
logrus.WithError(err).Errorf("slack error: %s", err.Error()) log.WithError(err).
WithField("channel", channel).
Errorf("slack error: %s", err.Error())
} }
return return