slack.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2017 Unknwon
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package clog
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. )
  22. const (
  23. SLACK = "slack"
  24. _SLACK_ATTACHMENT = `{
  25. "attachments": [
  26. {
  27. "text": "%s",
  28. "color": "%s"
  29. }
  30. ]
  31. }`
  32. )
  33. var slackColors = []string{
  34. "", // Trace
  35. "#3aa3e3", // Info
  36. "warning", // Warn
  37. "danger", // Error
  38. "#ff0200", // Fatal
  39. }
  40. type SlackConfig struct {
  41. // Minimum level of messages to be processed.
  42. Level LEVEL
  43. // Buffer size defines how many messages can be queued before hangs.
  44. BufferSize int64
  45. // Slack webhook URL.
  46. URL string
  47. }
  48. type slack struct {
  49. Adapter
  50. url string
  51. }
  52. func newSlack() Logger {
  53. return &slack{
  54. Adapter: Adapter{
  55. quitChan: make(chan struct{}),
  56. },
  57. }
  58. }
  59. func (s *slack) Level() LEVEL { return s.level }
  60. func (s *slack) Init(v interface{}) error {
  61. cfg, ok := v.(SlackConfig)
  62. if !ok {
  63. return ErrConfigObject{"SlackConfig", v}
  64. }
  65. if !isValidLevel(cfg.Level) {
  66. return ErrInvalidLevel{}
  67. }
  68. s.level = cfg.Level
  69. if len(cfg.URL) == 0 {
  70. return errors.New("URL cannot be empty")
  71. }
  72. s.url = cfg.URL
  73. s.msgChan = make(chan *Message, cfg.BufferSize)
  74. return nil
  75. }
  76. func (s *slack) ExchangeChans(errorChan chan<- error) chan *Message {
  77. s.errorChan = errorChan
  78. return s.msgChan
  79. }
  80. func buildSlackAttachment(msg *Message) string {
  81. return fmt.Sprintf(_SLACK_ATTACHMENT, msg.Body, slackColors[msg.Level])
  82. }
  83. func (s *slack) write(msg *Message) {
  84. attachment := buildSlackAttachment(msg)
  85. resp, err := http.Post(s.url, "application/json", bytes.NewReader([]byte(attachment)))
  86. if err != nil {
  87. s.errorChan <- fmt.Errorf("slack: %v", err)
  88. }
  89. defer resp.Body.Close()
  90. if resp.StatusCode/100 != 2 {
  91. data, _ := ioutil.ReadAll(resp.Body)
  92. s.errorChan <- fmt.Errorf("slack: %s", data)
  93. }
  94. }
  95. func (s *slack) Start() {
  96. LOOP:
  97. for {
  98. select {
  99. case msg := <-s.msgChan:
  100. s.write(msg)
  101. case <-s.quitChan:
  102. break LOOP
  103. }
  104. }
  105. for {
  106. if len(s.msgChan) == 0 {
  107. break
  108. }
  109. s.write(<-s.msgChan)
  110. }
  111. s.quitChan <- struct{}{} // Notify the cleanup is done.
  112. }
  113. func (s *slack) Destroy() {
  114. s.quitChan <- struct{}{}
  115. <-s.quitChan
  116. close(s.msgChan)
  117. close(s.quitChan)
  118. }
  119. func init() {
  120. Register(SLACK, newSlack)
  121. }