0

The SMTPHandler requires a "subject" to be included when it is initialized.

How can the subject line include the level of the event that generated the email? For example:

import logging
from logging.handlers import SMTPHandler

logger = logging.getLogger('test_logger')

email = SMTPHandler(*credentials, subject="class_name")

logger.addHandler(email)

logs.info('Info Test')

logs.error('Error Test')

How to make the subject list for logs.info('Info Test') be INFO: class_name and logs.Error('Error Test') be ERROR: class_name?

Greg
  • 8,175
  • 16
  • 72
  • 125
  • 1
    Possible duplicate of [Can Python's logging format be modified depending on the message log level?](https://stackoverflow.com/questions/1343227/can-pythons-logging-format-be-modified-depending-on-the-message-log-level) – wallyk Sep 10 '17 at 03:48
  • The two questions are different as the Formatted works for the content of the log message, and has nothing to do with the `SMTPHandler` subject line. – Greg Sep 10 '17 at 03:51

1 Answers1

1

To do this, you need to subclass SMTPHandler and override the getSubject method, which says:

If you want to specify a subject line which is record-dependent, override this method.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191