hY8vVpf3tyR57Xib
hY8vVpf3tyR57Xib

Reputation: 3915

Triggering python logger in Django project

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,
    },

Upvotes: 0

Views: 127

Answers (1)

Tim
Tim

Reputation: 1357

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

Upvotes: 3

Related Questions