pythonflaskpython-logging

How can I format Flask Werkzeug logging or move the request values to my logger format?


I am pretty green to Flask but not Python. I'm setting up my logging for my like so:

In logger.py:

logfile = f"{ basedir }/app.log"
log_format = '%(asctime)s - %(levelname)s - (%(threadName)-10s) - %(message)s'
logging.basicConfig(filename = logfile, format = log_format, level=logging.DEBUG)
log = logging.getLogger()

Then in each file:

from logger import log
log.debug("Oh no, the sky is falling!")

When my webapp gets an API call, it emits the following into the log:

2023-08-08 16:48:21,559 - INFO - (Thread-80 (process_request_thread)) - 111.222.333.444 - - [08/Aug/2023 16:48:21] "GET /v1/user/ HTTP/1.1" 200 -
                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The 1st half-ish is per my formatting. Werkzeug adds additional info to the end of the line, which I've noted. Note that the time/date is superfluous. I like the IP and request information.

How can I either:

a. Format that Werkzeug output to remove the time/date and possibly add additional meaningful values?

b. Just remove the Werkzeug output but add the IP and request info to the normal logger output?


Solution

  • Partially answering my own question....

    You can't change werkzeug logs. They are hardcoded in. See code here.