0

I have a Django project that has the following function in which I want to trigger a logger:

def remove_email_from_list(list_id, email_address):
    subscriber_hash = hashlib.md5(email_clean)
    url = '%s/lists/%s/members/%s' % (settings.API_URL, list_id, subscriber_hash.hexdigest())
    r = requests.delete(url, auth=HTTPBasicAuth('user', settings.API_KEY),)
    if r.status_code != 204:
        logging.critical("Executed mailchimp api call, wrong status code. Message body = " + r.text)
    return r

However, I tried all these loggers to catch this error and send an email, but somehow the logger isn't triggered. Do you guys know what I am doing wrong?

'loggers': {
    'django': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': True
    },
    'django.request': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': True
    },
    'project_name.logging': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': True,
    },
    'django.logging': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': True,
    },

1 Answer 1

3

I think if you call logging.critical directly, this is done via the default logger. Create a named logger first by using (somewhere in the head of your py-file):

logger = logging.getLogger(__name__)

and then call

logger.critical(...)

This should then use the logger with the package name of the file you are in

See Logger Objects Documentation

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm I am still having some trouble to configure this. This file (helper_functions.py) is located in the root of my application "mail". If I would define my logger as "api_logger", I should have to use the logger with "mail.api_logger"? That still doesn't catch my logs.
No, no not replace 'name' by anything custom. 'name' is automatically replaced with the modules dotted path. If you replace 'name' by 'api_logger' you would probably have to use the same name in the logger config.
Sorry, I did not mean to be rude and use a double 'no'. I actually meant to write 'No, do not', Also the double underscores were removed by the formating, sorry for that. should have been __name__

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.