thanks but i want to get console messages aswell ie messages like this
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
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.
even when i refresh the web page i start to get logs
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
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.
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 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)>
ok doing some tests, changed the D to S and interval 30 so it would rotate logs quicker and without
logger = logging.getLogger(__name__)
i get this
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
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.