slack.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "net/http"
  20. )
  21. const (
  22. SLACK = "slack"
  23. _SLACK_ATTACHMENT = `{
  24. "attachments": [
  25. {
  26. "text": "%s",
  27. "color": "%s",
  28. }
  29. ]
  30. }`
  31. )
  32. var slackColors = []string{
  33. "", // Trace
  34. "blue", // Info
  35. "warning", // Warn
  36. "danger", // Error
  37. "#ff0200", // Fatal
  38. }
  39. type SlackConfig struct {
  40. // Minimum level of messages to be processed.
  41. Level LEVEL
  42. // Buffer size defines how many messages can be queued before hangs.
  43. BufferSize int64
  44. // Slack webhook URL.
  45. URL string
  46. }
  47. type slack struct {
  48. Adapter
  49. url string
  50. }
  51. func newSlack() Logger {
  52. return &slack{
  53. Adapter: Adapter{
  54. quitChan: make(chan struct{}),
  55. },
  56. }
  57. }
  58. func (s *slack) Level() LEVEL { return s.level }
  59. func (s *slack) Init(v interface{}) error {
  60. cfg, ok := v.(SlackConfig)
  61. if !ok {
  62. return ErrConfigObject{"SlackConfig", v}
  63. }
  64. if !isValidLevel(cfg.Level) {
  65. return ErrInvalidLevel{}
  66. }
  67. s.level = cfg.Level
  68. if len(cfg.URL) == 0 {
  69. return errors.New("URL cannot be empty")
  70. }
  71. s.url = cfg.URL
  72. s.msgChan = make(chan *Message, cfg.BufferSize)
  73. return nil
  74. }
  75. func (s *slack) ExchangeChans(errorChan chan<- error) chan *Message {
  76. s.errorChan = errorChan
  77. return s.msgChan
  78. }
  79. func (s *slack) write(msg *Message) {
  80. attachment := fmt.Sprintf(_SLACK_ATTACHMENT, msg.Body, slackColors[msg.Level])
  81. if _, err := http.Post(s.url, "application/json", bytes.NewReader([]byte(attachment))); err != nil {
  82. s.errorChan <- fmt.Errorf("slack: %v", err)
  83. }
  84. }
  85. func (s *slack) Start() {
  86. LOOP:
  87. for {
  88. select {
  89. case msg := <-s.msgChan:
  90. s.write(msg)
  91. case <-s.quitChan:
  92. break LOOP
  93. }
  94. }
  95. for {
  96. if len(s.msgChan) == 0 {
  97. break
  98. }
  99. s.write(<-s.msgChan)
  100. }
  101. s.quitChan <- struct{}{} // Notify the cleanup is done.
  102. }
  103. func (s *slack) Destroy() {
  104. s.quitChan <- struct{}{}
  105. <-s.quitChan
  106. close(s.msgChan)
  107. close(s.quitChan)
  108. }
  109. func init() {
  110. Register(SLACK, newSlack)
  111. }