logging.rst 6.1 KB

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