Logging Basics in Django
In this page:
Getting a Logger in a View
Each module gets its own logger by name, usually set to __name__. Calling methods like .info() or .error() on it records a message at that severity level.
Example: Getting a Logger in a View
Each module gets its own logger by name, usually set to __name__. Calling methods like .info() or .error() on it records a message at that severity level.
import logging
logger = logging.getLogger(__name__)
def checkout_view(request):
logger.info('Checkout started for user %s', request.user.id)
return render(request, 'checkout.html')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Configuring LOGGING in settings.py
The LOGGING setting defines handlers (where messages go) and loggers (which messages get sent where, at what minimum level). A basic console handler is enough to get useful output during development.
Example: Configuring LOGGING in settings.py
The LOGGING setting defines handlers (where messages go) and loggers (which messages get sent where, at what minimum level). A basic console handler is enough to get useful output during development.
# settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {'console': {'class': 'logging.StreamHandler'}},
'root': {'handlers': ['console'], 'level': 'INFO'},
}
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Choosing the Right Log Level
DEBUG is for detailed diagnostic info during development, INFO for routine events, WARNING for something unexpected but non-fatal, and ERROR for a failure that needs attention. Picking the right level keeps logs useful instead of noisy.
Note: Reserve ERROR and above for things that actually need someone's attention.
Example: Choosing the Right Log Level
DEBUG is for detailed diagnostic info during development, INFO for routine events, WARNING for something unexpected but non-fatal, and ERROR for a failure that needs attention. Picking the right level keeps logs useful instead of noisy.
import logging
logger = logging.getLogger(__name__)
def process_payment(order):
logger.debug('Processing payment for order %s', order.id)
if order.total <= 0:
logger.warning('Order %s has a non-positive total', order.id)
return False
logger.info('Payment succeeded for order %s', order.id)
return True
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Using print() statements for debugging instead of the logging module, which disappears once the server isn't run from a visible terminal.
- Logging everything at the same level, making it impossible to tell routine info from real errors.
- Never configuring a logging handler, so log messages silently go nowhere in production.
- Django's logging is configured through the LOGGING dictionary in settings.py, built on Python's standard logging module.
- Log messages have levels (DEBUG, INFO, WARNING, ERROR, CRITICAL) indicating their severity.
- Handlers decide where log messages go, such as the console or a file.
- Using logging instead of print() gives you levels, timestamps, and control over where messages end up.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: