# No debug messages going into log file

**URL:** <https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897>\
**Category:** Python Help\
**Created:** [July 11, 2024, 12:32pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897 "2024-07-11T12:32:31Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![robina80](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/robina80/32/20142_2.png) [@robina80](https://discuss.python.org/u/robina80)\
**Post date:** [July 11, 2024, 12:32pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/1 "2024-07-11T12:32:31Z")

</div>

hi all,

the below code works

```python
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.DEBUG)
handler = TimedRotatingFileHandler(filename='password.log', when='S', interval=30, backupCount=5, delay=True)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s', datefmt='%Y/%m/%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)

```

but none of the debug messages are going in the log file

thanks,  
rob

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [July 11, 2024, 1:40pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/2 "2024-07-11T13:40:12Z")

</div>

The logging level must be set on handlers too.

---

<div class="post-metadata">

**Author:** ![robina80](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/robina80/32/20142_2.png) [@robina80](https://discuss.python.org/u/robina80)\
**Post date:** [July 11, 2024, 1:49pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/3 "2024-07-11T13:49:20Z")

</div>

you mean like this

```python
handler.setLevel(logging.DEBUG)

```

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [July 11, 2024, 1:59pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/4 "2024-07-11T13:59:27Z")

</div>

Yep.

> **[logging — Logging facility for Python](https://docs.python.org/3/library/logging.html#logging.Handler.setLevel)**
>
> Source code: Lib/logging/\_\_init\_\_.py Important: This page contains the API reference information. For tutorial information and discussion of more advanced topics, see Basic Tutorial, Advanced Tutor...

If that still doesn’t worked, I got it working without much hassle, using an ordinary FileHandler, not that rotating one.

---

<div class="post-metadata">

**Author:** ![robina80](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/robina80/32/20142_2.png) [@robina80](https://discuss.python.org/u/robina80)\
**Post date:** [July 11, 2024, 2:27pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/5 "2024-07-11T14:27:39Z")

</div>

> [@robina80](#):
>
> `handler.setLevel(logging.DEBUG)`

still not working, still not seeing debug messages ie messages that i see on console, i want to log the messages that i see in console aswell

```python
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.DEBUG)
handler = TimedRotatingFileHandler(filename='password.log', when='D', interval=1, backupCount=5, delay=True)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s', datefmt='%Y/%m/%d %H:%M:%S')
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)

```

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [July 11, 2024, 4:15pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/6 "2024-07-11T16:15:21Z")

</div>

I don’t know how to use that TimedRotating thing but this:

```python
import logging

logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='password.log')
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s', datefmt='%Y/%m/%d %H:%M:%S')
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.debug('Test debug message')

```

produces:

```python
>py logging_test.py

>more password.log
2024/07/11 17:14:07:DEBUG:Test debug message

```

---

<div class="post-metadata">

**Author:** ![robina80](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/robina80/32/20142_2.png) [@robina80](https://discuss.python.org/u/robina80)\
**Post date:** [July 11, 2024, 6:17pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/7 "2024-07-11T18:17:18Z")

</div>

thanks but i want to get console messages aswell ie messages like this

```python
python password.py
 * Serving Flask app 'password'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on https://127.0.0.1:5000
 * Running on https://10.100.1.10:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 118-343-405
127.0.0.1 - - [11/Jul/2024 18:08:05] "POST /password HTTP/1.1" 200 -
127.0.0.1 - - [11/Jul/2024 18:08:07] "POST /password HTTP/1.1" 200 -
127.0.0.1 - - [11/Jul/2024 18:08:08] "POST /password HTTP/1.1" 200 -
127.0.0.1 - - [11/Jul/2024 18:08:09] "POST /password HTTP/1.1" 200 -

```

ive created a couple of logger.info and logger.warning and they work fine, i just want the console messages to be redirected to the log file aswell

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [July 11, 2024, 6:22pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/8 "2024-07-11T18:22:10Z")

</div>

That’s just not what a logger does–it doesn’t redirect all of `stdout` or `stderr` to a file. It writes _logs_ to a file.

If you want to record all the console output, I’d do that as part of invocation, e.g. `python password.py > all_the_output.log 2>&1` will direct stdout to the file with `>` and redirects stderr to stdout (and thus to the file) with `2>&1`.

This won’t rotate the log files the way the file handler does, however.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [July 11, 2024, 7:30pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/9 "2024-07-11T19:30:34Z")

</div>

Connect one of these:

> **[contextlib — Utilities for with-statement contexts](https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout)**
>
> Source code: Lib/contextlib.py This module provides utilities for common tasks involving the with statement. For more information see also Context Manager Types and With Statement Context Managers....

to a subclass of one of these:

> **[io — Core tools for working with streams](https://docs.python.org/3/library/io.html#io.TextIOBase)**
>
> Source code: Lib/io.py Overview: The io module provides Python’s main facilities for dealing with various types of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These ar...

on which the `write` method calls `logger.info`

---

<div class="post-metadata">

**Author:** ![robina80](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/robina80/32/20142_2.png) [@robina80](https://discuss.python.org/u/robina80)\
**Post date:** [July 12, 2024, 6:28am UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/10 "2024-07-12T06:28:06Z")

</div>

this is what i mean, when i use this config in my code

```python
logger = logging.getLogger( __name__ )
logging.basicConfig(filename='password.log', format='%(asctime)s:%(levelname)s:%(message)s', datefmt='%d/%m/%Y %H:%M:%S', encoding='utf-8', level=logging.DEBUG)

```

even when i refresh the web page i start to get logs

```python
12/07/2024 07:25:03:INFO:e[31me[1mWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.e[0m
 * Running on all addresses (0.0.0.0)
 * Running on https://127.0.0.1:5000
 * Running on https://10.100.1.10:5000
12/07/2024 07:25:03:INFO:e[33mPress CTRL+C to quite[0m
12/07/2024 07:25:03:INFO: * Restarting with stat
12/07/2024 07:25:03:WARNING: * Debugger is active!
12/07/2024 07:25:03:INFO: * Debugger PIN: 118-343-405

```

i dont understand why “RotatingFileHandler” doesnt do it but “basicConfig” does

its like its paying no attention to my debug lines

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [July 12, 2024, 8:05am UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/11 "2024-07-12T08:05:18Z")

</div>

You do know storing PINs or any credentials in log files is a security vulnerability - it’s just as bad as storing passwords in there?

But anyway. It sounds like Flask is writing log messages, not just printing to stdout. But Flask doesn’t know about the `logger` you created - its logging messages will possibly first go to its own logger, then be passed upwards to the root logger. Logs do not go down to your child logger.

As it says in the [manual](https://docs.python.org/3/library/logging.html#logging.basicConfig):

> Does basic configuration for the logging system by creating a [`StreamHandler`]… adding it to the root logger.

A reference to the root logger is returned by [`getLogger`](https://docs.python.org/3/library/logging.html#logging.getLogger) if called with no args. Add your handlers to that instead.

---

<div class="post-metadata">

**Author:** ![robina80](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/robina80/32/20142_2.png) [@robina80](https://discuss.python.org/u/robina80)\
**Post date:** [July 16, 2024, 2:17pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/12 "2024-07-16T14:17:45Z")

</div>

sorry for the late reply, still doesnt work

```python
logger = logging.getLogger( __name__ )
logger.setLevel(logging.DEBUG)
handler = TimedRotatingFileHandler(filename='password.log', when='D', interval=1, backupCount=5, delay=True)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s', datefmt='%Y/%m/%d %H:%M:%S')
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)

```

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [July 16, 2024, 2:39pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/13 "2024-07-16T14:39:11Z")

</div>

`logger = logging.getLogger( __name__ )` creates a Module or script level logger, not a reference to the Root logger, which is required, as Flask only knows about that logger (and perhaps its own internal loggers, but that’s not the common pattern).

Please reread the last sentence of my previous post.

```python
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logger = logging.getLogger()
>>> logger
<RootLogger root (WARNING)>

```

---

<div class="post-metadata">

**Author:** ![robina80](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/robina80/32/20142_2.png) [@robina80](https://discuss.python.org/u/robina80)\
**Post date:** [July 17, 2024, 4:33pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/14 "2024-07-17T16:33:15Z")

</div>

you mean like this

```python
logger = logging.getLogger()

```

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [July 17, 2024, 6:00pm UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/15 "2024-07-17T18:00:18Z")

</div>

Yes - exactly.

---

<div class="post-metadata">

**Author:** ![robina80](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/robina80/32/20142_2.png) [@robina80](https://discuss.python.org/u/robina80)\
**Post date:** [July 18, 2024, 10:53am UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/16 "2024-07-18T10:53:11Z")

</div>

nice thank you @JamesParrott it works !!!

```python
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = TimedRotatingFileHandler(filename='password.log', when='D', interval=1, backupCount=5, delay=True)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s', datefmt='%Y/%m/%d %H:%M:%S')
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)

```

---

<div class="post-metadata">

**Author:** ![robina80](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/robina80/32/20142_2.png) [@robina80](https://discuss.python.org/u/robina80)\
**Post date:** [July 18, 2024, 11:06am UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/17 "2024-07-18T11:06:59Z")

</div>

ok doing some tests, changed the D to S and interval 30 so it would rotate logs quicker and without

```python
logger = logging.getLogger( __name__ )

```

i get this

```python
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\python\\venv\\password.log' -> 'C:\\python\\venv\\password.log.2024-07-18_12-04-16'

```

so it looks like i need the “( **name** )” otherwise i get that error

i just think this will never work with the “TimedRotatingFileHandler”

but my issue is in production if my script comes into problems i wont know whats wrong with it to fix it

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [July 18, 2024, 11:45am UTC](https://discuss.python.org/t/no-debug-messages-going-into-log-file/57897/18 "2024-07-18T11:45:15Z")

</div>

Yep logging’s complicated. I’ve only used Python’s `logging` to optionally help users capture debug output, in an app. On my own prod servers I just containerise each service and let Docker and the host OS’s systemd / journald handle logging.

I don’t use Arch by the way, but this feature was useful: [https://wiki.archlinux.org/title/Systemd/Journal#Journal\_size\_limit](https://wiki.archlinux.org/title/Systemd/Journal#Journal_size_limit)
