notice.go 960 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package logs
  2. import (
  3. "bp.jydev.jianyu360.cn/BP/jynsq/gonsq"
  4. "encoding/json"
  5. "log"
  6. )
  7. type NoticeConfig struct {
  8. isOpen bool //是否开启提示
  9. IsJsonEncode bool
  10. Addr, topic string
  11. Id, Title, Text string
  12. }
  13. type Notice struct {
  14. p *gonsq.Producer
  15. c NoticeConfig
  16. }
  17. func newNotice(c NoticeConfig) (n *Notice, err error) {
  18. n = &Notice{
  19. c: c,
  20. }
  21. var producer *gonsq.Producer
  22. if c.isOpen {
  23. producer, err = gonsq.NewProducer(c.Addr, c.topic, c.IsJsonEncode)
  24. if err != nil {
  25. return
  26. }
  27. n.p = producer
  28. }
  29. return
  30. }
  31. // SendWarnMsg 发送异常通知
  32. func (n *Notice) SendWarnMsg(detail map[string]interface{}) error {
  33. if n.p == nil {
  34. return nil
  35. }
  36. m := &gonsq.Msg{n.c.Id, n.c.Title, n.c.Text, detail}
  37. bs, err := json.Marshal(m)
  38. if err != nil {
  39. return err
  40. }
  41. log.Println("模拟发送信息----->", string(bs))
  42. return nil
  43. if err := n.p.Publish(bs); err != nil {
  44. log.Println("nsq连接失败", err)
  45. }
  46. return nil
  47. }