# Restart Django logging files once runserver is executed...

**URL:** <https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611>\
**Category:** Using Django\
**Created:** [May 25, 2020, 1:24am UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611 "2020-05-25T01:24:08Z")\
**Posts on this page:** 15\
**Page:** 1

<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:** [May 25, 2020, 1:24am UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/1 "2020-05-25T01:24:08Z")

</div>

I have django logging configured and working. My question is: Is it possible to wipe the files once the runserver command is run? I am not sure if Django has a built-in feature for this.

Thanks!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 25, 2020, 3:38am UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/2 "2020-05-25T03:38:28Z")

</div>

I’ve never tried this, but the [logging configuration documentation](https://docs.python.org/3/library/logging.html?highlight=filemode#logging.basicConfig) gives me the impression that you can specify the `filemode` parameter in your handler with a `'w'` to indicate overwrite rather than append.

I’m guessing that this could be an issue if you’re doing this during development, because I’m guessing it would reset the file every time the app was restarted, so if you’re using runserver (or runserver\_plus) or even uwsgi with file-change detection, it’s going to empty the file every time a file is changed. (On the other hand, this may be _exactly_ what you want.)

---

<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:** [May 25, 2020, 4:07am UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/3 "2020-05-25T04:07:07Z")

</div>

I was trying to find a way to implement this, but only upon running the server so I can start fresh with the logs when I test my project. File mode ‘w’ would work, but I’m afraid that would wipe the file every time my code hits a new log statement. I wasn’t sure if there was a way to state “wipe\_on\_server\_start” using the LOGGING dict in the Django settings file.

---

<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:** [May 25, 2020, 4:12am UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/4 "2020-05-25T04:12:20Z")

</div>

With that being said, I could just make a custom command to wipe the logs, create any necessary db rows for development, and run the server all in one go.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [May 25, 2020, 6:41am UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/5 "2020-05-25T06:41:22Z")

</div>

You could also wipe the log files at server start by using a custom subclass of `FileHandler` whose ` __init__ ` erases the file.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 25, 2020, 9:53am UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/6 "2020-05-25T09:53:32Z")

</div>

Just as a follow-up, I confirmed that setting `'mode': 'w'` on a logging.FileHandler works as I had guessed it might. It will log whatever it’s supposed to log, until runserver is restarted - at which point the file is truncated.

So as long as nothing happens to restart runserver, your log will continue to accumulate data.

---

<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:** [May 25, 2020, 4:23pm UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/7 "2020-05-25T16:23:16Z")

</div>

That’s sounds like exactly what I need. Thank you for the 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:** [May 25, 2020, 8:31pm UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/8 "2020-05-25T20:31:34Z")

</div>

Another quick question: Where would I put ‘mode’:‘w’? I’m assuming in the LOGGER dict at some point, but would it go under the loggers sub-dict or handlers sub-dict?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 25, 2020, 8:37pm UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/9 "2020-05-25T20:37:01Z")

</div>

It’s a handler’s setting. This is an excerpt from my test case this morning that I used to try this:

```auto
'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': '/var/logs/test/dt_debug.log',
        'formatter': 'verbose',
        'mode': 'w',
    },
 },

```

Ken

---

<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:** [May 25, 2020, 8:40pm UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/10 "2020-05-25T20:40:37Z")

</div>

Yep. That did exactly what I needed. Thanks, man!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 25, 2020, 8:45pm UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/11 "2020-05-25T20:45:50Z")

</div>

Thanks for the question! I learned something new today - and I think I’m going to find this handy as well.

Normally, we use the RotatingFileHandler, which can make it difficult to find stuff. With this, I can add a second log file for just the current run so that I’m only looking at log data since the most recent change - and if I ever want to reset the file, I can just touch the settings file to cause it to be reinitialized.

---

<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:** [May 25, 2020, 8:47pm UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/12 "2020-05-25T20:47:50Z")

</div>

I was initially using RotatingFileHandler, but also found it hard to navigate between which log file I needed. I decided this approach was more appropriate for a dev environment, especially for testing. I used Django professionally for about 3 years in college and my roommate and I are currently working on a startup 🙂 Trying to make sure we keep the dev cycle super clean, concise, and scalable while also paying close attention to test-driven development.

---

<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:** [May 25, 2020, 8:49pm UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/13 "2020-05-25T20:49:29Z")

</div>

Also, I’m not sure if you ever ran into this problem, but I found a way to separate model fields and model functions into separate files while still being able to perform Django queries correctly.

I wanted to organize the files because my model files started to grow very quickly when custom model methods were introduced.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 25, 2020, 9:41pm UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/14 "2020-05-25T21:41:22Z")

</div>

I’ve seen someone else here recommend that idea (separate Models from Functions). I don’t get it. I don’t see the appeal or how it helps. By definition, an object is the data and the methods that operate on that data. The idea of splitting those two just “smells” bad to me.

I’m much more inclined to split my models.py into a models module and have the ` __init__.py` file import them all from the subordinate files. But we don’t even start to consider that until the models.py file gets larger than about 2000 lines.

---

<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:** [May 25, 2020, 9:54pm UTC](https://forum.djangoproject.com/t/restart-django-logging-files-once-runserver-is-executed/2611/15 "2020-05-25T21:54:10Z")

</div>

2000 lines i way too much for me, personally. I think it all comes down to personal preference and how the developer plans to support their code base. I personally like the appeal of having an inheritance structure to better organize my files (fields and functions) so i can keep the aforementioned definition of an object true.
