std.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package notify
  2. import (
  3. "time"
  4. "github.com/labring/aiproxy/core/common/config"
  5. "github.com/labring/aiproxy/core/common/trylock"
  6. log "github.com/sirupsen/logrus"
  7. )
  8. type StdNotifier struct{}
  9. var (
  10. infoLogrus = log.WithField("notify", "std")
  11. warnLogrus = log.WithField("notify", "std")
  12. errorLogrus = log.WithField("notify", "std")
  13. )
  14. func (n *StdNotifier) Notify(level Level, title, message string) {
  15. note := config.GetNotifyNote()
  16. switch level {
  17. case LevelInfo:
  18. logrus := infoLogrus.WithField("title", title)
  19. if note != "" {
  20. logrus = logrus.WithField("note", note)
  21. }
  22. logrus.Info(message)
  23. case LevelWarn:
  24. logrus := warnLogrus.WithField("title", title)
  25. if note != "" {
  26. logrus = logrus.WithField("note", note)
  27. }
  28. logrus.Warn(message)
  29. case LevelError:
  30. logrus := errorLogrus.WithField("title", title)
  31. if note != "" {
  32. logrus = logrus.WithField("note", note)
  33. }
  34. logrus.Error(message)
  35. }
  36. }
  37. func (n *StdNotifier) NotifyThrottle(
  38. level Level,
  39. key string,
  40. expiration time.Duration,
  41. title, message string,
  42. ) {
  43. if !trylock.MemLock(key, expiration) {
  44. return
  45. }
  46. n.Notify(level, title, message)
  47. }