mixins.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. import logging
  3. logger = logging.getLogger('django-cqrs')
  4. class LoggingMixin:
  5. _BASE_PAYLOAD_LOG_TEMPLATE = 'CQRS is %s: pk = %s (%s), correlation_id = %s.'
  6. @staticmethod
  7. def log_consumed(payload):
  8. """
  9. Args:
  10. payload (dj_cqrs.dataclasses.TransportPayload): Transport payload from master model.
  11. """
  12. msg = 'CQRS is received: pk = %s (%s), correlation_id = %s.'
  13. logger.info(msg, payload.pk, payload.cqrs_id, payload.correlation_id)
  14. @staticmethod
  15. def log_consumed_accepted(payload):
  16. """
  17. Args:
  18. payload (dj_cqrs.dataclasses.TransportPayload): Transport payload from master model.
  19. """
  20. msg = 'CQRS is applied: pk = %s (%s), correlation_id = %s.'
  21. logger.info(msg, payload.pk, payload.cqrs_id, payload.correlation_id)
  22. @staticmethod
  23. def log_consumed_denied(payload):
  24. """
  25. Args:
  26. payload (dj_cqrs.dataclasses.TransportPayload): Transport payload from master model.
  27. """
  28. msg = 'CQRS is denied: pk = %s (%s), correlation_id = %s.'
  29. logger.warning(msg, payload.pk, payload.cqrs_id, payload.correlation_id)
  30. @staticmethod
  31. def log_consumed_failed(payload):
  32. """
  33. Args:
  34. payload (dj_cqrs.dataclasses.TransportPayload): Transport payload from master model.
  35. """
  36. msg = ('CQRS is failed: pk = %s (%s), correlation_id = %s, retries = %s.',)
  37. logger.warning(
  38. msg,
  39. payload.pk,
  40. payload.cqrs_id,
  41. payload.correlation_id,
  42. payload.retries,
  43. )
  44. @staticmethod
  45. def log_dead_letter(payload):
  46. """
  47. Args:
  48. payload (dj_cqrs.dataclasses.TransportPayload): Transport payload from master model.
  49. """
  50. msg = 'CQRS is added to dead letter queue: pk = %s (%s), correlation_id = %s.'
  51. logger.warning(msg, payload.pk, payload.cqrs_id, payload.correlation_id)
  52. @staticmethod
  53. def log_delayed(payload, delay, eta):
  54. """
  55. Args:
  56. payload (dj_cqrs.dataclasses.TransportPayload): Transport payload from master model.
  57. delay (int): Seconds to wait before requeuing message.
  58. eta (datetime): Requeuing datetime.
  59. """
  60. msg = ('CQRS is delayed: pk = %s (%s), correlation_id = %s, delay = %s sec, eta = %s.',)
  61. logger.warning(
  62. msg,
  63. payload.pk,
  64. payload.cqrs_id,
  65. payload.correlation_id,
  66. delay,
  67. eta,
  68. )
  69. @staticmethod
  70. def log_requeued(payload):
  71. """
  72. Args:
  73. payload (dj_cqrs.dataclasses.TransportPayload): Transport payload from master model.
  74. """
  75. msg = ('CQRS is requeued: pk = %s (%s), correlation_id = %s.',)
  76. logger.warning(msg, payload.pk, payload.cqrs_id, payload.correlation_id)
  77. @staticmethod
  78. def log_produced(payload):
  79. """
  80. Args:
  81. payload (dj_cqrs.dataclasses.TransportPayload): Transport payload from master model.
  82. """
  83. msg = 'CQRS is published: pk = %s (%s), correlation_id = %s.'
  84. logger.info(msg, payload.pk, payload.cqrs_id, payload.correlation_id)