# Need clarification including logging in Django

**URL:** <https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463>\
**Category:** Using Django\
**Created:** [September 22, 2020, 3:52pm UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463 "2020-09-22T15:52:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [September 22, 2020, 3:52pm UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463/1 "2020-09-22T15:52:30Z")

</div>

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 `settings.py` 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!

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [September 22, 2020, 4:17pm UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463/2 "2020-09-22T16:17:03Z")

</div>

> **[Logging | Django documentation | Django](https://docs.djangoproject.com/en/3.1/topics/logging/)**

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

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [September 22, 2020, 6:58pm UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463/3 "2020-09-22T18:58:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [September 22, 2020, 7:03pm UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463/4 "2020-09-22T19:03:49Z")

</div>

> [@nemecek\_f](#):
>
> 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?

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

---

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [September 22, 2020, 7:08pm UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463/5 "2020-09-22T19:08:27Z")

</div>

In my experiments I have this code:

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

logger = logging.getLogger( __name__ )
logger.addHandler(f_handler)

```

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.

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [September 22, 2020, 7:14pm UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463/6 "2020-09-22T19:14:58Z")

</div>

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 `settings.py` 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.

---

<div class="post-metadata">

**Author:** ![thecubist](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thecubist/32/1271_2.png) [@thecubist](https://forum.djangoproject.com/u/thecubist)\
**Post date:** [September 23, 2020, 9:19am UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463/7 "2020-09-23T09:19:13Z")

</div>

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
logger.info("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

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!

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [September 23, 2020, 12:32pm UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463/8 "2020-09-23T12:32:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [September 23, 2020, 7:20pm UTC](https://forum.djangoproject.com/t/need-clarification-including-logging-in-django/4463/9 "2020-09-23T19:20:16Z")

</div>

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
