logging.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. .. _logging:
  2. Red Mail Logging Handlers
  3. =========================
  4. Red Mail also has logging handlers which
  5. work with the *logging* library from Python's
  6. standard library. You may also use the `SMTPHandler <https://docs.python.org/3/library/logging.handlers.html#logging.handlers.SMTPHandler>`_
  7. from logging library but Red Mail provides more
  8. customizable alternatives. Red Mail also has a
  9. handler that is capable of sending multiple
  10. records at once.
  11. EmailHandler
  12. ------------
  13. To send one email per log record, use :class:`EmailHandler`.
  14. .. code-block:: python
  15. import logging
  16. from redmail import EmailHandler
  17. hdlr = EmailHandler(
  18. host="localhost",
  19. port=0,
  20. subject="A log record",
  21. receivers=["me@example.com"],
  22. )
  23. logger = logging.getLogger(__name__)
  24. logger.addHandler(hdlr)
  25. .. note::
  26. You may pass the :class:`EmailSender`
  27. directly as an argument ``email``, for
  28. example:
  29. .. code-block:: python
  30. from redmail import EmailSender
  31. hdlr = EmailHandler(
  32. email=EmailSender(host="localhost", port=0)
  33. subject="A log record",
  34. receivers=["me@example.com"],
  35. )
  36. Note that a copy of the :class:`EmailSender` is created
  37. in order to avoid affecting the usage of the instance
  38. elsewhere. Additional arguments (such as subject, sender,
  39. receivers, text, html, etc.) are set as attributes to
  40. this copy.
  41. Then to use this, simply:
  42. .. code-block:: python
  43. logger.warning("A warning happened")
  44. You may also customize the subject to include the level name (ie. info, debug, etc.):
  45. .. code-block:: python
  46. hdlr = EmailHandler(
  47. host="localhost",
  48. port=0,
  49. subject="Log Record: {record.levelname}",
  50. receivers=["me@example.com"],
  51. )
  52. logger = logging.getLogger(__name__)
  53. logger.addHandler(hdlr)
  54. You may also customize the subject and the bodies
  55. .. code-block:: python
  56. import logging
  57. from redmail import EmailHandler
  58. hdlr = EmailHandler(
  59. host="localhost",
  60. port=0,
  61. subject="Log Record: {record.levelname}",
  62. receivers=["me@example.com"],
  63. text="Logging level: {{ record.levelname }}\nMessage: {{ msg }}",
  64. html="<ul><li>Logging level: {{ record.levelname }}</li><li>Message: {{ msg }}</li></ul>",
  65. )
  66. logger = logging.getLogger(__name__)
  67. logger.addHandler(hdlr)
  68. The following arguments are passed to the string format:
  69. ======== =====
  70. Argument Type Description
  71. ======== =====
  72. record logging.LogRecord
  73. handler EmailHandler
  74. ======== =====
  75. And the passed Jinja parameters:
  76. ======== =====
  77. Argument Type Description
  78. ======== =====
  79. record logging.LogRecord Log record
  80. msg str Formatted message
  81. handler EmailHandler Handler itself
  82. ======== =====
  83. MultiEmailHandler
  84. -----------------
  85. In case sending emails after each log record is too much, you may use :class:`MultiEmailHandler`
  86. that sends the log records via email after specific number of log records have occurred, when
  87. manually flushed or using custom logic.
  88. A simple example:
  89. .. code-block:: python
  90. import logging
  91. from redmail import MultiEmailHandler
  92. hdlr = MultiEmailHandler(
  93. capacity=2, # Sends email after every second record
  94. host="localhost",
  95. port=0,
  96. subject="log records",
  97. receivers=["me@example.com"],
  98. )
  99. logger = logging.getLogger(__name__)
  100. logger.addHandler(hdlr)
  101. Then to use this, simply:
  102. .. code-block:: python
  103. logger.warning("A warning happened")
  104. logger.warning("A warning happened")
  105. # Should have now sent an email
  106. # Manually flush
  107. logger.warning("A warning happened")
  108. hdlr.flush()
  109. .. note::
  110. You may pass the :class:`EmailSender`
  111. directly as an argument ``email``, for
  112. example:
  113. .. code-block:: python
  114. from redmail import EmailSender
  115. hdlr = MultiEmailHandler(
  116. email=EmailSender(host="localhost", port=0)
  117. subject="Log records",
  118. receivers=["me@example.com"],
  119. )
  120. Note that a copy of the :class:`EmailSender` is created
  121. in order to avoid affecting the usage of the instance
  122. elsewhere. Additional arguments (such as subject, sender,
  123. receivers, text, html, etc.) are set as attributes to
  124. this copy.
  125. The following arguments are passed to the subject format:
  126. ======== =====
  127. Argument Type Description
  128. ======== =====
  129. records list of logging.LogRecord
  130. min_level_name str Name of the lowest log level name
  131. max_level_name str Name of the highest log level name
  132. handler EmailHandler
  133. ======== =====
  134. And the passed Jinja parameters:
  135. ======== =====
  136. Argument Type Description
  137. ======== =====
  138. record logging.LogRecord Log record
  139. msg str Formatted message
  140. handler EmailHandler Handler itself
  141. ======== =====