Skip to main content

AdminEmailHandler

An exception log handler that emails log entries to site admins.

If the request is passed as the first argument to the log record, request data will be provided in the email report.

Attributes

AttributeTypeDescription
include_htmlboolean = FalseBoolean flag that determines whether to include an HTML version of the exception traceback in the email report.
email_backendstring = NoneThe Python path to the email backend class to use for sending emails, deprecated in favor of the 'using' attribute.
usingstring = NoneThe name of the mailer alias defined in settings to use for sending admin emails.
reporter_classstring = settings.DEFAULT_EXCEPTION_REPORTERThe exception reporter class used to generate the text and HTML bodies of the error notification email.

Constructor

Signature

def AdminEmailHandler(
include_html: boolean = False,
email_backend: str = None,
reporter_class: str = None,
using: str = None
) - > null

Parameters

NameTypeDescription
include_htmlboolean = FalseWhether to include an HTML attachment containing a full traceback in the email.
email_backendstr = NoneThe Python path to a specific email backend class to use (deprecated).
reporter_classstr = NoneThe Python path to the exception reporter class to use for generating the email body.
usingstr = NoneThe name of the mailer alias to use for sending emails.

Methods


emit()

@classmethod
def emit(
record: LogRecord
) - > null

Processes a log record by generating an email report and sending it to site administrators. It extracts request metadata, formats the traceback using the configured reporter class, and handles both plain text and optional HTML content.

Parameters

NameTypeDescription
recordLogRecordThe logging record object containing the message, level, and optional request or exception information to be emailed.

Returns

TypeDescription
nullNothing is returned; the method performs an I/O operation by sending an email.

send_mail()

@classmethod
def send_mail(
subject: string,
message: string,
*args: any,
**kwargs: any
) - > null

Sends the formatted log entry to the administrators defined in the project settings. It utilizes the configured email backend or mailer alias to dispatch the message.

Parameters

NameTypeDescription
subjectstringThe summary line for the email, typically containing the log level and source IP.
messagestringThe plain text body of the email containing the log message and traceback.
*argsanyAdditional positional arguments passed to the underlying mail delivery function.
**kwargsanyAdditional keyword arguments, such as 'html_message', used to customize the email delivery.

Returns

TypeDescription
nullNothing is returned; the method triggers the mail delivery process.

format_subject()

@classmethod
def format_subject(
subject: string
) - > string

Escape CR and LF characters.

Parameters

NameTypeDescription
subjectstringThe raw subject string that may contain prohibited newline characters.

Returns

TypeDescription
stringThe sanitized subject string with newline and carriage return characters replaced by literal escape sequences.