|
|
|
@ -18,6 +18,7 @@ import (
|
|
|
|
|
"bytes" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"io/ioutil" |
|
|
|
|
"net/http" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
@ -27,7 +28,7 @@ const (
|
|
|
|
|
"attachments": [ |
|
|
|
|
{ |
|
|
|
|
"text": "%s", |
|
|
|
|
"color": "%s", |
|
|
|
|
"color": "%s" |
|
|
|
|
} |
|
|
|
|
] |
|
|
|
|
}` |
|
|
|
@ -35,7 +36,7 @@ const (
|
|
|
|
|
|
|
|
|
|
var slackColors = []string{ |
|
|
|
|
"", // Trace
|
|
|
|
|
"blue", // Info
|
|
|
|
|
"#3aa3e3", // Info
|
|
|
|
|
"warning", // Warn
|
|
|
|
|
"danger", // Error
|
|
|
|
|
"#ff0200", // Fatal
|
|
|
|
@ -93,10 +94,16 @@ func (s *slack) ExchangeChans(errorChan chan<- error) chan *Message {
|
|
|
|
|
|
|
|
|
|
func (s *slack) write(msg *Message) { |
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
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() { |
|
|
|
|