|
|
@ -18,6 +18,7 @@ import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"io/ioutil" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -27,7 +28,7 @@ const ( |
|
|
|
"attachments": [ |
|
|
|
"attachments": [ |
|
|
|
{ |
|
|
|
{ |
|
|
|
"text": "%s", |
|
|
|
"text": "%s", |
|
|
|
"color": "%s", |
|
|
|
"color": "%s" |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
] |
|
|
|
}` |
|
|
|
}` |
|
|
@ -35,7 +36,7 @@ const ( |
|
|
|
|
|
|
|
|
|
|
|
var slackColors = []string{ |
|
|
|
var slackColors = []string{ |
|
|
|
"", // Trace
|
|
|
|
"", // Trace
|
|
|
|
"blue", // Info
|
|
|
|
"#3aa3e3", // Info
|
|
|
|
"warning", // Warn
|
|
|
|
"warning", // Warn
|
|
|
|
"danger", // Error
|
|
|
|
"danger", // Error
|
|
|
|
"#ff0200", // Fatal
|
|
|
|
"#ff0200", // Fatal
|
|
|
@ -93,10 +94,16 @@ func (s *slack) ExchangeChans(errorChan chan<- error) chan *Message { |
|
|
|
|
|
|
|
|
|
|
|
func (s *slack) write(msg *Message) { |
|
|
|
func (s *slack) write(msg *Message) { |
|
|
|
attachment := fmt.Sprintf(_SLACK_ATTACHMENT, msg.Body, slackColors[msg.Level]) |
|
|
|
attachment := fmt.Sprintf(_SLACK_ATTACHMENT, msg.Body, slackColors[msg.Level]) |
|
|
|
if _, err := http.Post(s.url, "application/json", bytes.NewReader([]byte(attachment))); err != nil { |
|
|
|
resp, err := http.Post(s.url, "application/json", bytes.NewReader([]byte(attachment))) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
s.errorChan <- fmt.Errorf("slack: %v", err) |
|
|
|
s.errorChan <- fmt.Errorf("slack: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode/100 != 2 { |
|
|
|
|
|
|
|
data, _ := ioutil.ReadAll(resp.Body) |
|
|
|
|
|
|
|
s.errorChan <- fmt.Errorf("slack: %s", data) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *slack) Start() { |
|
|
|
func (s *slack) Start() { |
|
|
|