Need clarification including logging in Django

Hello! I would like to implement proper logging in my Django app. So far I have read the docs and few articles I found but I am not sure if I understand correctly how to use the loggers.

Examples frequently show code like this:

logger = logging.getLogger(__name__)

This should get me logger with the name of the module, which is nice, but I don’t understand how to configure its handlers and level.

On the other hand other examples show LOGGING configuration in where you define your loggers by name and can attach different handlers.

So the real usage maybe consists of having a helper file with function that will give me handler with correct configuration? Or am I missing something?

I don’t think I need to log by specific module but it would be useful to somehow know from where was particular message logged.

Thanks for help!

Here is the official Django documentation on proper logging within a Django application. This should guide you on how to add logging within your file as well as setting up handlers, formatters and filters (if needed).

Hope this helps :slight_smile:

Yeah this is exactly what I read but I did not answer all the questions I have.

Okay, no problem! Just for clarification, what did you mean by this?

In my experiments I have this code:

f_handler = logging.FileHandler('file.log')

logger = logging.getLogger(__name__)

And with this handler my logger would work in a very basic sense writing into file.log. So my thinking is that I would need something similar in all the py files where I want to log, so I was thinking about creating a helper that would just return me configured logger, maybe with __name__ as input so I can filter based on modules.

This isn’t necessary with Django’s built-in logging functionality. Django can handle the creation of logging objects for you by configuring them in your file.

You can create formatters that will automatically format the log output for you, handlers that dictate where the log message is displayed (i.e. to the console or a specified log file), filters so you can designate the base log level, and loggers that will perform the logging action.

Hi @nemecek_f,

when I started using Django, I found its logging behavior quite confusing.

It’s all in the documentation, but here’s the basic idea:

You’d like to log a message? Import logging and go ahead:

# In your app, e.g. named "todo"
import logging
logger = logging.getLogger() # Name isn't needed necessarily"A log info message")

That’s all you need to do. Next, you’ll need to configure logging handlers, that define

  • (a) which log level(s) you care about, in which apps (in your case, probably the “todo” app) – these are loggers
  • (b) how to log them (in a file, as an email, as a console message, …) – these are handlers

A typical case would be to set up two handlers:

  • One handler that logs all messages (that are at least INFOs) to a file
  • One handler that sends an email to the project admin when WARNING and ERROR messages are logged

import logging

        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'mail_admins': {
                'level': 'WARNING',
                'class': 'django.utils.log.AdminEmailHandler',

            'file': {
                'level': 'INFO',
                'class': 'logging.FileHandler',
                'filename': '/path/to/django/info.log',
        'loggers': {
            'todo': {
                'level': 'INFO', # Info or higher
                'handlers': ['file', 'mail_admins' ],
                'propagate': True,

Hope this helps!

1 Like

Great explanation. I’m relatively new to the forums so I’m trying to figure out where the line is with regards to “leading someone in the right direction” or “blatantly giving them the answer”, if that makes sense. Thanks for the clarification.

Thank you both! I have done some experiments and I feel like I have much better grasp regarding logging now. Next challenge is to email admins about serious