Primary Menu

How to make django management commands send errors email to admins

I usually use django’s custom management commands to do different tasks on the web application, most of the times with a cron job running those commands. The problem is that when you’re using a cron job, you won’t get the beautiful django error email when something goes wrong.

You can of course log all the commands you run and the cron jobs you have in files, but the problem is that sometimes you need to be notified instantly via your email, so here’s how to make django management commands send you the errors email.

Understanding how django’s error email works

If you check django’s logging documentation you will find that it has a built-in handler called AdminEmailHandler this handler sends an email to the site admins for each log message it receives.

So basically all you need to do is to tell your django commands to log the errors using a logger with that handler, and you’ll receive the emails.

To do so define a logger in your
settings.py:

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

LOGGING={

#OTHER OPTIONS

'filters':{

#OTHER FILTERS

'require_debug_false':{

'()':'django.utils.log.RequireDebugFalse'

}

},

'handlers':{

#OTHER HANDLERS

'mail_admins':{

'level':'ERROR',

'filters':['require_debug_false'],

'class':'django.utils.log.AdminEmailHandler',

'include_html':True

}

},

'loggers':{

#OTHER LOGGERS

'management_commands':{

'handlers':['mail_admins'],

'level':'ERROR',

'propagate':True,

},

}

}

And after that in your custom commands log the errors you face using the logger you defined: