middleware.go 669 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package middleware
  2. import (
  3. "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client/router"
  4. "git.sxidc.com/service-supports/fslog"
  5. "runtime/debug"
  6. "slices"
  7. "time"
  8. )
  9. func Logger(skipTopics []string) router.Handler {
  10. return func(item *router.Item, data []byte) {
  11. start := time.Now()
  12. item.Next()
  13. if slices.Contains(skipTopics, item.Topic) {
  14. return
  15. }
  16. end := time.Now()
  17. fslog.Info("| %d | %s |",
  18. end.Sub(start),
  19. item.Topic,
  20. )
  21. }
  22. }
  23. func Recovery() router.Handler {
  24. return func(item *router.Item, data []byte) {
  25. defer func() {
  26. err := recover()
  27. if err != nil {
  28. fslog.Error("%s", debug.Stack())
  29. }
  30. }()
  31. item.Next()
  32. }
  33. }