Skip to main content

SES log handler

Log messages to email via Amazon SES

Rate limiting and bouncing are currently not supported.

If you are using Django then this is also possible using Django's AdminEmailHandler and django-ses which supports rate limiting via AWS_SES_AUTO_THROTTLE.

Quick start

Installation

pip install ses-log-handler

If you using IAM roles to get credentials for AWS or have the correct environment variables defined (see Boto3 configuration guide) then you can simply set a sender and recipients addresses for the handler:

import logging

logger = logging.getLogger(__name__)


ses_handler = SESHandler(
    sender='from@example.com',
    recipients=['to@example.com'],
)
ses_handler.setLevel(logging.ERROR)
logger.addHandler(ses_handler)

There is an example of how to configure the log hander using dictConfig() and fileConfig()

Note: It is advised you set the log level to either CRITICAL or ERROR. This will stop the reciver_mails from being spammed by logs and you incuring a large SES bill.

Configuration options

If you want to explicitly set the access, secret and region this can also be when instantiating the SESHandler.

It also possible to force the subject line to be the same for each email

mail_handler = SESHandler(
    sender='from@example.com',
    recipients=('to@example.com'),
    access_key='<access-key>',
    secret_key='<secret-key>',
    region='<region>',
    subject='Error: Exception raised',
)

sender

Required: Who the log email should be sent from. The domain should be added and configured as a verified sender domain

recipients

Required: A list of recipients who should receive the log emails.

If your SES account only has "sandbox" access (see SES dashboard) these email addresses must be added as verified sender email address

access_key

Optional: The AWS access key for a user who has access to send emails via SES.

It is best practices for managing AWS access keys use instance roles instead of passing access keys to your application.

If the access_key argument is not provided then the SESHandler (which uses boto3) will fall back to getting credentials from either the instance role or environment variables. See the boto3 credentials guide for more information.

secret_key

Optional: The AWS secret key for a user who has access to send emails via SES.

It is best practices for managing AWS access keys use instance roles instead of passing access keys to your application.

If the access_key argument is not provided then the SESHandler (which uses boto3) will fall back to getting credentials from either the instance role or environment variables. See the boto3 credentials guide for more information.

region

Optional: The AWS region which should be used to send emails via SES

By default the region that your application is running in will be used

subject

Optional: Force overrides the subject line to be the same for each email.

The default is <log_level>: <message>

Configuration examples

The below examples both create a logging config which logs everything ERROR and above to SES and anything WARNING and below to the console.

Dict config

import logging

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'general': {
            'format': '%(asctime)s %(name)s.%(funcName)s %(levelname)s [%(lineno)d] %(message)s',  # NOQA: E501
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'general',
            'level': 'INFO',
        },
        'ses': {
            'class': 'ses_log_handler.SESHandler',
            'formatter': 'general',
            'level': 'ERROR',
            'sender': 'from@example.com',
            'recipients': ['to@example.com'],
        },
    },
    'root': {
        'level': 'INFO',
        'handlers': ['ses', 'console'],
   }
}
logging.config.dictConfig(LOGGING)

File config

[loggers]
keys=root

[handlers]
keys=sesHandler,consoleHandler

[formatters]
keys=generalFormatter

[logger_root]
level=INFO
handlers=sesHandler,consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=generalFormatter
args=(sys.stdout,)

[handler_sesHandler]
class=ses_log_handler.SESHandler
level=ERROR
formatter=generalFormatter
args=['from@example.com', ['to@example.com']]

[formatter_generalFormatter]
format=%(asctime)s %(name)s.%(funcName)s %(levelname)s [%(lineno)d] %(message)s

Then to load this config you can use

import logging

logging.config.fileConfig(
    'logging.conf',
    disable_existing_loggers=False
)

Metadata

Release files for ses-log-handler 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ses-log-handler 1.0.2
File Size Uploaded Provenance Actions
ses-log-handler-1.0.2.tar.gz 4.7 kB Without provenance Details for ses-log-handler-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ses-log-handler 1.0.2
File Interpreter ABI Platform Actions
ses_log_handler-1.0.2-py3-none-any.whl Python 3 none any Details for ses_log_handler-1.0.2-py3-none-any.whl

Total release size: 8.8 kB

Release files / ses-log-handler-1.0.2.tar.gz

Download URL ses-log-handler-1.0.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4975548ab70e5c9b0e067af18dc8eaa74835beb2bb7bc88885862a419093467e
BLAKE2b-256 checksum
How to use checksums
47410a55830022e65ac53dc9c579ec578ab0de2cb568427aa92945284e688d8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.6.10 Darwin/18.6.0

Release files / ses_log_handler-1.0.2-py3-none-any.whl

Download URL ses_log_handler-1.0.2-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
030b952aa9892dd44b851341b61cdc6f0f18162799c5d0f82bc4f3aea80033c1
BLAKE2b-256 checksum
How to use checksums
c76062ec1fc55661c04627ec06575bb4a4a33a958f13e28040211995e2619b46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.6.10 Darwin/18.6.0

Security for ses-log-handler 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page