django-anymail icon indicating copy to clipboard operation
django-anymail copied to clipboard

Reduced DEBUG_API_REQUESTS

Open pgcd opened this issue 3 years ago • 2 comments

Just a suggestion: would it be possible to add a "reduced" DEBUG_API_REQUESTS implementation to log only basic information about the requests and responses (eg. status code, possibly errors in the response; a subset of fields in the request) to allow use outside dev environments? I'm asking because we had an impossible to track intermittent problem with some notifications (ESP is Postmark) - all our logs confirmed that the email was sent, but nothing was apparently received by Postmark, and Anymail showed no error.

pgcd avatar Nov 04 '22 11:11 pgcd

Long-term I'm thinking Anymail should really embrace the Python logging facility, replacing DEBUG_API_REQUESTS. So log.info every send, with the sort of minimal info you describe. And log.debug the full request/response (what DEBUG_API_REQUESTS does now). Probably log webhooks the same way. Maybe even log.warning any errors ignored by Django's fail_silently send option.

And then the standard Django LOGGING settings would control it all, allowing users to change the log level for the anymail module (or even individual ESP backends, I guess).

Short-term, for requests-based backends (all but Amazon SES), you could use the new requests session customization hook that will be in Anymail 9.0 to add your own logging of anything you want:

def log_response(response):
    # do any logging you want here...
    # response is a requests.Response
    request = response.request  # request is a requests.PreparedRequest
    print(request.url, response.status_code)

class MyDebugPostmarkBackend(anymail.backends.postmark.EmailBackend):
    def create_session(self):
        session = super().create_session()
        session.hooks['response'].append(log_response)
        return session

(And then in settings.py, set EMAIL_BACKEND = 'path.to.your.MyDebugPostmarkBackend'.

medmunds avatar Nov 05 '22 00:11 medmunds

Excellent, thanks - both are perfectly viable solutions, looking forward to them.

pgcd avatar Nov 05 '22 11:11 pgcd