slack.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 (s *slack) write(msg *Message) {
  81. attachment := fmt.Sprintf(_SLACK_ATTACHMENT, msg.Body, slackColors[msg.Level])
  82. resp, err := http.Post(s.url, "application/json", bytes.NewReader([]byte(attachment)))
  83. if err != nil {
  84. s.errorChan <- fmt.Errorf("slack: %v", err)
  85. }
  86. defer resp.Body.Close()
  87. if resp.StatusCode/100 != 2 {
  88. data, _ := ioutil.ReadAll(resp.Body)
  89. s.errorChan <- fmt.Errorf("slack: %s", data)
  90. }
  91. }
  92. func (s *slack) Start() {
  93. LOOP:
  94. for {
  95. select {
  96. case msg := <-s.msgChan:
  97. s.write(msg)
  98. case <-s.quitChan:
  99. break LOOP
  100. }
  101. }
  102. for {
  103. if len(s.msgChan) == 0 {
  104. break
  105. }
  106. s.write(<-s.msgChan)
  107. }
  108. s.quitChan <- struct{}{} // Notify the cleanup is done.
  109. }
  110. func (s *slack) Destroy() {
  111. s.quitChan <- struct{}{}
  112. <-s.quitChan
  113. close(s.msgChan)
  114. close(s.quitChan)
  115. }
  116. func init() {
  117. Register(SLACK, newSlack)
  118. }