Flask by default will log things like GET and POST requests directly with an INFO tag. When implementing a custom logger, these are posted to the same logger and clutter my INFO layer. Is there a way to downgrade them to another layer like DEBUG?
This is the logger I use:
# create logger
FORMAT = '%(asctime)s - %(module)s - %(levelname)s - Thread_name: %(threadName)s - %(message)s'
logging.basicConfig(
format=FORMAT, datefmt='%m/%d/%Y %I:%M:%S %p',
filename='wizard/logs/example.log', level=logging.DEBUG)
I'm not sure about a way to downgrade log level of requests (as it usually is stated explicitly in the code like logging.info("...")) but the following may help you to reduce the verbosity of Flask itself.
Python allows you to have multiple loggers, each with its own log level. You can modify any existing logger if you know the module name it is in or the name it was registered with as described here.
For example:
import logging
logger = logging.getLogger("mypackage.mymodule") # or __name__ for current module
logger.setLevel(logging.ERROR)
The above can be done for any python module. Flask provides a logger per app. You can get a reference to it like this:
import logging
from flask import Flask
app = Flask(__name__) # or instead of __name__ provide the name of the module
app.logger.setLevel(logging.ERROR)
In fact, there is a way to downgrade a log record from INFO to DEBUG (even if it was already emitted using a call such as info()
). This can be achieved using a filter attached to a logger. According to the docs a filter checks:
Is the specified record to be logged? Returns zero for no, nonzero for yes. If deemed appropriate, the record may be modified in-place by this method.
So a filter may change the level of a log record (levelno and levelname attributes). Later on, a handler may then allow or drop this record based on the new level.
Just in case anyone visits this place,
I'm facing the same issue as well. It seems that print calls have the tendency to 'not run'. But if you use the logger instead of print you will find that codes are actually still running.
Not quite sure what the reason is, but when other sections of my code starts running suddenly all the old prints will appear.
tldr; don't use print, use logger
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With