Django Log Shell And Dbshell
title: Logging Django shell and dbshell sessions tags: django logging —
The Django shell management command is a supremely useful tool for developers to explore and potentially modify data in their applications, both during development and in production. For jobs where that does not perform well enough, or you need lower level access to the data, the dbshell command gives you even more power.
With great power, comes great responsibility
Every developer has been in a situation where they use their supremely powerful tools to make a supremely large mistake against production data. In those cases, although it may not save you, it can be critical to have a log of what you did, exactly. If you realize your mistake while you are in front of the console, they you have all the information you need. But what if you only realize minutes, hours or days later?
Or perhaps you just need plausible deniability. In either case, it’s not too hard to hookup logging to Django’s shell commands. In this post, I’ll show you how to enable per-user logging into separate files, with date and time stamps.
Shell Logging with iPython
iPython is a great replacement for the already pretty awesome interactive interpreter built into python. You should be running it anyway, but if you’re looking for another reason, it has built-in logging capability. To get Django to use iPython for its shell
command, you just need to have iPython installed with pip install ipython
. Django will detect and use it.
To enable logging from Django’s shell
command, put the following in you project root directory in a file called ipython_config.py
.
To get shell
to recognize your config file, you need to write your own subclass. Place this in a file under your project directory called management/commands/shell.py
, and Django will automatically pick it up. Make sure to create __init__.py
files in the management
and commands
directories.
You will also need to create the /var/log/django
directory with write permissions.
Database Shell Logging with MySQL
Assuming you’re using MySQL as your database, you can enable logging by placing the following code in your settings.py
file, bellow your DATABASES
declaration.
This method also uses a logging facility built-in to an external tool, in this case the mysql
command line interactive shell.